diff options
Diffstat (limited to 'nixos/rebuild')
| -rwxr-xr-x | nixos/rebuild | 52 |
1 files changed, 28 insertions, 24 deletions
diff --git a/nixos/rebuild b/nixos/rebuild index 80a52cd..9e62b33 100755 --- a/nixos/rebuild +++ b/nixos/rebuild @@ -1,31 +1,35 @@ #!/usr/bin/env bash set -euo pipefail -# Enable all future paths in this script to be relative to the directory containing the script. -cd "$(dirname -- "${BASH_SOURCE[0]}")" - -configPath=$(realpath -- "hosts/$HOSTNAME/default.nix") +top=$(dirname "$(realpath "$0")") +configPath="$top/hosts/$HOSTNAME/default.nix" if [ ! -f $configPath ]; then - echo "aborting: $configPath doesn't exist" - exit 1 -fi - -firstLine=$(head -n1 "$configPath") - -if ! echo "$firstLine" | grep -E ^"# channel *=" > /dev/null; then - echo "aborting: $configPath doesn't start with \`# channel=\"...\"\`, where ... is a pin from sources.json" - exit 1 + echo "aborting: $configPath doesn't exist" + exit 1 fi -nix-eval() { - nix-instantiate --eval --read-write-mode "$@" \ - | tr -d \" # nix-instantiate has no raw output yet (like the experimental nix eval) -} - -channel=$(nix-eval --argstr line "$(echo $firstLine | tr -d \#)" --expr '{line}: (builtins.fromTOML line).channel') -nixpkgsPath=$(nix-eval --argstr channel "$channel" --expr '{channel}: (import ./npins).${channel}.outPath') - -# nixos-rebuild always reads Nixpkgs from the NIX_PATH, -# so we need to set it explicitly to our pinned version -exec nixos-rebuild "$@" -I nixpkgs="$nixpkgsPath" -I nixos-config="$configPath" -I top="$(pwd)" +nixpkgsPath=$( + cd "$top" && + nix-instantiate --eval --raw \ + --argstr host "$HOSTNAME" \ + --expr ' + { host }: + let + toml = builtins.readFile ./hosts/${host}/metadata.toml; + channel = (builtins.fromTOML toml).channel; + in + (import ./npins).${channel}.outPath + ' +) + +( + # The result symlink created in the current directory is registered as a root of the Nix + # garbage collector, so it's important that we call nixos-rebuild from the host directory. + # (nix-build has an --out-link option but that option cannot be set via nixos-rebuild). + cd $top/hosts/$HOSTNAME && + + # nixos-rebuild always reads Nixpkgs from the NIX_PATH, + # so we need to set it explicitly to our pinned version + exec nixos-rebuild "$@" -I nixpkgs="$nixpkgsPath" -I nixos-config="$configPath" -I top="$top" +) |
