-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackage.nix
147 lines (135 loc) · 4.23 KB
/
package.nix
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
{
lib,
makeScope,
newScope,
stdenv,
writeShellApplication,
nixos-rebuild,
openssh,
}: let
rebuildOpts = lib.cli.toGNUCommandLineShell {} {
fast = true;
use-remote-sudo = true;
use-substitutes = true;
};
nixOpts = lib.cli.toGNUCommandLineShell {} {
max-jobs = "auto";
cores = 0;
};
shellApp = name: commandLine: {
${name} = writeShellApplication {
inherit name;
runtimeInputs = [nixos-rebuild];
text = ''
die() {
echo "$*" >&2
exit 1
}
DEPLOY_HOST=
FLAKE_TARGET=
NIX_OPTIONS=( )
while [ "$#" -gt 0 ]; do
if [ -z "$DEPLOY_HOST" ]; then
DEPLOY_HOST="$1"
shift
elif [ "''${1:0:1}" = "-" ]; then
break;
elif [ -z "$FLAKE_TARGET" ]; then
FLAKE_TARGET="$1"
shift
else
break
fi
done
[ -z "$DEPLOY_HOST" ] && die "no target given"
[ -z "$FLAKE_TARGET" ] && FLAKE_TARGET="''${DEPLOY_HOST##*@}" # remove a leading 'user@' stanza
NIX_OPTIONS=("''${@:1}") # any remaining options are nix options
# avoid unused variable warnings
export DEPLOY_HOST
export FLAKE_TARGET
export NIX_OPTIONS
set -x
${commandLine}
'';
};
};
in
makeScope newScope (self:
##
# All scripts take:
#
# $1 : user@machine:
# user@machine : stanza given to SSH for remote access
# machine : name of NixOS system declared in flake
# ${@:2} : nix options
# options passed to nix as-is
##
lib.concatMapAttrs shellApp {
# build machine locally
# ... remember `'$` escape oddity
"build" = ''
nixos-rebuild build \
${rebuildOpts} --flake ".#$FLAKE_TARGET" \
${nixOpts} "''${NIX_OPTIONS[@]}"
'';
# build machine remotely
"build-there" = ''
nixos-rebuild build \
${rebuildOpts} --build-host "$DEPLOY_HOST" --target-host "$DEPLOY_HOST" \
--flake ".#$FLAKE_TARGET" \
${nixOpts} "''${NIX_OPTIONS[@]}"
'';
# build machine locally, apply locally
"switch" = ''
nixos-rebuild switch \
${rebuildOpts} --flake ".#$FLAKE_TARGET" \
${nixOpts} "''${NIX_OPTIONS[@]}"
'';
# build machine remotely, apply remotely
"switch-pull" = ''
nixos-rebuild switch \
${rebuildOpts} --build-host "$DEPLOY_HOST" --target-host "$DEPLOY_HOST" \
--flake ".#$FLAKE_TARGET" \
${nixOpts} "''${NIX_OPTIONS[@]}"
'';
# build machine locally, apply remotely
"switch-push" = ''
nixos-rebuild switch \
${rebuildOpts} --target-host "$DEPLOY_HOST" --flake ".#$FLAKE_TARGET" \
${nixOpts} "''${NIX_OPTIONS[@]}"
'';
}
// {
# timeout-loop waiting for successful ssh
ssh-wait = writeShellApplication {
name = "ssh-wait";
runtimeInputs = [openssh];
text = ''
while ! ssh -o ConnectTimeout=5 -o ServerAliveInterval=5 "$@"; do
echo "$(date) ssh-wait: $*"
done
'';
};
# switch-pull, followed by a reboot and a nix-collect-garbage
switch-pull-reset = writeShellApplication {
name = "switch-pull-reset";
runtimeInputs = with self; [ssh-wait switch-pull];
text = /*bash*/ ''
switch-pull "$@"
ssh "$1" "sudo reboot && while echo \"\$(date): waiting for reboot\"; do sleep 1; done" || true
sleep 1 # patience: sometimes machines will *still* allow reconnect
ssh-wait "$1" "sudo nix-collect-garbage --delete-older-than 15d"
'';
};
# switch-push, followed by a reboot and a nix-collect-garbage
switch-push-reset = writeShellApplication {
name = "switch-push-reset";
runtimeInputs = with self; [ssh-wait switch-push];
text = ''
switch-push "$@"
ssh "$1" "sudo reboot && while echo \"\$(date): waiting for reboot\"; do sleep 1; done" || true
sleep 1 # patience: sometimes machines will *still* allow reconnect
ssh-wait "$1" "sudo nix-collect-garbage --delete-older-than 15d"
'';
};
})