]> Git — Sourcephile - julm/julm-nix.git/blob - flake/apps/switch.nix
compat(nix): do not require to build with flake
[julm/julm-nix.git] / flake / apps / switch.nix
1 {
2 build,
3 config,
4 hostName,
5 lib,
6 nixpkgsPath,
7 profile,
8 writeShellApplication,
9 coreutils,
10 nix,
11 systemd,
12 }:
13 writeShellApplication {
14 name = "${hostName}-switch";
15 runtimeInputs = [
16 coreutils
17 ];
18 text = ''
19 # shellcheck disable=SC2027
20 # shellcheck disable=SC2035
21 # shellcheck disable=SC2086
22 set -eux
23 shopt -s globstar
24
25 chmod -R g-rwx,o-rwx ./**/*.gpg
26 trap 'git reset ./**/*.gpg' EXIT
27 git rm -rf --cached --ignore-unmatch ./**/*.gpg # prevent copying to /nix/store
28
29 nix-store --add-root hosts/${hostName}.nixpkgs --indirect --realise ${nixpkgsPath}
30 nix-store --add-root hosts/${hostName}.root --indirect --realise ${build.toplevel}
31
32 nix copy --to ssh://${config.install.target}"?''${targetStore-}"${lib.optionalString config.install.substituteOnDestination " --substitute-on-destination"} ${build.toplevel}
33
34 if ssh ${config.install.target} set -x ';' \
35 systemctl reset-failed nixos-fallback '2>/dev/null' ';' \
36 test "''${NO_NIXOS_FALLBACK:+set}" '||' \
37 systemd-run -u nixos-fallback --description=nixos-fallback /bin/sh -xc '''\'''
38 PATH=${
39 lib.makeBinPath [
40 coreutils
41 nix
42 systemd
43 ]
44 }
45 sleep $((10 * 60))
46 ${profile}/bin/switch-to-configuration switch
47 systemctl reboot
48 '\'''' '&&' \
49 ${build.toplevel}/bin/switch-to-configuration test
50 then
51 ssh ${config.install.target} -o ControlPath=none set -x ';' \
52 systemctl stop nixos-fallback.service ';' \
53 nix-env --profile ${profile} --set '${build.toplevel}' ';' \
54 ${build.toplevel}/bin/switch-to-configuration boot '&&' \
55 nix-env --delete-generations 7d --profile ${profile}
56 else
57 tput rev
58 echo WARNING: switch-to-configuration was not registered at boot
59 tput sgr0
60 ssh ${config.install.target} -o ControlPath=none set -x ';' \
61 systemctl stop nixos-fallback.service
62 false
63 fi
64 '';
65 }