blob: 9b256149d9418586ac641f97ac42302d6629d98a (
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
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
|
#!/usr/bin/env bash
source $(dirname ${BASH_SOURCE[0]})/nix-utils.sh
CONFIG_DIR=
COMMAND="switch"
usage() {
cat <<EOS
$(help_synopsis "${BASH_SOURCE[0]}" "[-h] [-c <command>] [-g <git command>] -w <working directory> [-- args...]")
-c <command> Command for nixos-rebuild. See 'man nixos-rebuild'
-g <git cmd> Alternative git commit, defaults to 'tag -a'
-w <path> Path to your configuration git directory
-n Include hostname in tag name
-h Show this help and exit
Everything after a double dash (--) will be passed to nixos-rebuild as
additional parameters. For example:
nix-script switch -c switch -- -I nixpkgs=/home/user/pkgs
$(help_end)
EOS
}
COMMAND=
ARGS=
WD=
TAG_NAME=
GIT_COMMAND=
HOSTNAME=""
while getopts "c:w:t:nh" OPTION
do
case $OPTION in
c)
COMMAND=$OPTARG
stdout "COMMAND = $COMMAND"
;;
w)
WD=$OPTARG
stdout "WD = $WD"
;;
t)
TAG_NAME=$OPTARG
stdout "TAG_NAME = $TAG_NAME"
;;
g)
GIT_COMMAND=$OPTARG
stdout "GIT_COMMAND = $GIT_COMMAND"
;;
n)
HOSTNAME=$(hostname)
stdout "HOSTNAME = $HOSTNAME"
;;
h)
usage
exit 1
;;
esac
done
ARGS=$(echo $* | sed -r 's/(.*)(\-\-(.*)|$)/\2/')
stdout "ARGS = $ARGS"
[[ -z "$WD" ]] && \
stderr "No configuration git directory." && \
stderr "Won't do anything" && exit 1
[[ ! -d "$WD" ]] && stderr "No directory: $WD" && exit 1
[[ -z "$COMMAND" ]] && COMMAND="switch"
[[ -z "$GIT_COMMAND" ]] && GIT_COMMAND="tag -a"
explain sudo nixos-rebuild $COMMAND $ARGS
REBUILD_EXIT=$?
if [[ $REBUILD_EXIT -eq 0 ]]
then
LASTGEN=$(current_system_generation)
sudo -k
if [[ -z "$TAG_NAME" ]]
then
if [[ -z "$HOSTNAME" ]]; then TAG_NAME="nixos-$LASTGEN-$COMMAND"
else TAG_NAME="nixos-$HOSTNAME-$LASTGEN-$COMMAND"
fi
fi
explain git --git-dir="$WD/.git" --work-tree="$WD" $GIT_COMMAND "$TAG_NAME"
else
stderr "Switching failed. Won't executing any further commands."
exit $REBUILD_EXIT
fi
|