summaryrefslogtreecommitdiff
path: root/nixos/rebuild
blob: cfc14cf639bf9bb9e2a56e3050f454e17aab3fb4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/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")

if [ ! -f $configPath ]; then
    echo "aborting: $configPath doesn't exist"
    exit 1
fi

nix-eval() {
    # FUTURE: use --raw when upgrading to NixOS 25.05
    nix-instantiate --eval --read-write-mode "$@" \
      | tr -d \"  # nix-instantiate has no raw output yet (like the experimental nix eval)
}

channel=$(nix-eval --argstr host "$HOSTNAME" --expr '{host}:
let
  toml = builtins.readFile ./hosts/${host}/metadata.toml;
in
  (builtins.fromTOML toml).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)"