Minor build improvements

Not sure if the features come from the new nixos-rebuild but good this
simplifies things.
This commit is contained in:
Geoffrey Frogeye 2025-12-06 13:08:53 +01:00
parent 66885ab02e
commit b170a1581d
3 changed files with 29 additions and 15 deletions

10
flake.lock generated
View file

@ -784,15 +784,15 @@
}, },
"zelbarnixpkgs": { "zelbarnixpkgs": {
"locked": { "locked": {
"lastModified": 1748415083, "lastModified": 1743964214,
"narHash": "sha256-jmIRxOA7kj1CFNiP6S6pg6e6XeQi3whBVvWsATpEiC4=", "narHash": "sha256-tPYh5TeRDOV9qMMr6VrgAOa5ZScqc4bohEDIU2grCnc=",
"owner": "GeoffreyFrogeye", "owner": "wlcx",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "bc0e8ee7d08241e8fbcb5e13a7abb445329e5644", "rev": "53524ff74074e3670c9a34729aa343dd461bfdb4",
"type": "github" "type": "github"
}, },
"original": { "original": {
"owner": "GeoffreyFrogeye", "owner": "wlcx",
"ref": "zelbar", "ref": "zelbar",
"repo": "nixpkgs", "repo": "nixpkgs",
"type": "github" "type": "github"

View file

@ -5,6 +5,9 @@
(pkgs.writeShellApplication { (pkgs.writeShellApplication {
name = "rb"; name = "rb";
text = '' text = ''
info() {
echo -e '\033[1;34m'"$*"'\033[0m'
}
verb="switch" verb="switch"
if [ "$#" -ge 1 ] if [ "$#" -ge 1 ]
then then
@ -14,7 +17,9 @@
nixos_flake="$(readlink -f /etc/nixos)" nixos_flake="$(readlink -f /etc/nixos)"
if [ -f "$nixos_flake/flake.nix" ] if [ -f "$nixos_flake/flake.nix" ]
then then
info "Updating local flakes"
update-local-flakes "$nixos_flake" update-local-flakes "$nixos_flake"
info "Starting script"
nix run "$nixos_flake#nixosRebuild" -- "$verb" "$@" nix run "$nixos_flake#nixosRebuild" -- "$verb" "$@"
fi fi
# TODO Fix nix-on-droid and home-manager # TODO Fix nix-on-droid and home-manager

View file

@ -24,18 +24,23 @@ then
exit 2 exit 2
fi fi
nixosRebuild() {
# sudo so the eval cache is shared with nixos-rebuild
sudo nixos-rebuild --flake "$self#$HOSTNAME" "$@" --log-format internal-json |& nom --json
}
info "Evaluating" info "Evaluating"
# Evaluating can take a lot of memory, and Nix doesn't free it until the program ends, # Evaluating can take a lot of memory, and Nix doesn't free it until the program ends,
# which can be limiting on memory-constrained devices. Hence the build step is separate. # which can be limiting on memory-constrained devices. Hence the build step is separate.
# Drawback: it will query info about missing paths twice # Drawback: it will query info about missing paths twice
# nix eval doesn't use the eval cache, so we do a nix build --dry-run nixosRebuild dry-build
# sudo so the eval cache is shared with nixos-rebuild
json=$(time sudo nix build "$self#nixosConfigurations.$HOSTNAME.config.system.build.toplevel" --dry-run --json )
toplevel=$(echo "$json" | jq '.[0].outputs.out' -r)
derivation=$(echo "$json" | jq '.[0].drvPath' -r)
info "Building" info "Building"
sudo nom build "$derivation^*" --no-link "$@" tmpdir="$(mktemp --suffix -dry-build -d)"
(cd "$tmpdir" && nixosRebuild build)
toplevel="$(readlink -f "$tmpdir"/result)"
rm "$tmpdir"/result
rmdir "$tmpdir"
info "Showing diff" info "Showing diff"
nvd diff "$(readlink -f /nix/var/nix/profiles/system)" "$toplevel" nvd diff "$(readlink -f /nix/var/nix/profiles/system)" "$toplevel"
@ -59,9 +64,12 @@ then
if [ -n "$specialisation" ] if [ -n "$specialisation" ]
then then
specialisationArgs=("--specialisation" "$specialisation") specialisationArgs=("--specialisation" "$specialisation")
echo "Currently running specialisation: $specialisation"
else
echo "Currently running unspecialized system"
fi fi
else else
warn "Could not find link for $currentSystem, will switch to non-specialized system" warn "Could not find link for $currentSystem, will switch to unspecialized system"
fi fi
@ -75,9 +83,10 @@ then
fi fi
if [ "$verb" = "test" ] || [ "$verb" = "switch" ] || [ "$confirm" = "y" ] if [ "$verb" = "test" ] || [ "$verb" = "switch" ] || [ "$confirm" = "y" ]
then then
info "Applying" info "Installing passwords"
"$toplevel/update-password-store" "$toplevel/update-password-store"
sudo nixos-rebuild --flake "$self#$HOSTNAME" test "${specialisationArgs[@]}" "$@" info "Applying"
nixosRebuild test "${specialisationArgs[@]}" "$@"
fi fi
# Set as boot # Set as boot
@ -89,5 +98,5 @@ fi
if [ "$verb" = "boot" ] || [ "$verb" = "switch" ] || [ "$confirm" = "y" ] if [ "$verb" = "boot" ] || [ "$verb" = "switch" ] || [ "$confirm" = "y" ]
then then
info "Setting as boot" info "Setting as boot"
sudo nixos-rebuild --flake "$self#$HOSTNAME" boot "$@" nixosRebuild boot "$@"
fi fi