about summary refs log tree commit diff
path: root/activate
blob: ee1c6bdce6c0fc613749a756a6f23ae06e0e2b67 (plain) (blame)
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
31
32
#! /usr/bin/env bash

# IMPORTANT: Don't use nix-shell shebang!
# If a config has generated an invalid NIX_PATH, it still needs to be
# possible to rescue the system by activating a different configuration.

set -ueo pipefail

d="$(dirname $0)"

if [ "$#" -eq 0 ]; then
    sys="$(hostname -s)"

    if ! [ -f "sys/$sys.nix" ]; then
        echo -e "\e[31;1merror:\e[0m must specify a system definition" >&2
        echo "Available system definitions are:" >&2
        find $d/sys -name '*.nix' -not -name default.nix | awk -F / '{print "- " $NF}' | sed 's/\.nix$//' >&2
        exit 1
    fi
else
    sys="$1"
    shift
fi

export NIX_PATH=nixos-config=$d/sys/$sys.nix:$d
linkdir="$(mktemp -d)"
link="$linkdir/result"
nix-build -A system --out-link "$link" '<nixpkgs/nixos>' "$@"
path="$(readlink "$link")"
nix-env -p /nix/var/nix/profiles/system --set "$path"
rm -rf "$linkdir"
exec "$path/bin/switch-to-configuration" switch