summaryrefslogtreecommitdiffstats
path: root/nix-script-switch.sh
blob: 749b929823e3cc3606911ba1aa1fb22343f57695 (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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/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] [-q] [-c <command>] [-w <working directory>] [-- args...]")

        -c <command>    Command for nixos-rebuild. See 'man nixos-rebuild' (default: switch)
        -w <path>       Path to your configuration git directory (default: '$RC_CONFIG')
        -n              DON'T include hostname in tag name
        -t <tagname>    Custom tag name
        -p [<pkgs>]     Generate the switch tag in the nixpkgs at <pkgs> as well. (default: '$RC_NIXPKGS')
        -f <tag-flags>  Flags for git-tag (see 'git tag --help') (default: '$RC_SWITCH_DEFAULT_TAG_FLAGS')
        -b              Do not call nixos-rebuild at all.
        -q              Don't pass -Q to nixos-rebuild
        -h              Show this help and exit

        This command helps you rebuilding your system and keeping track
        of the system generation in the git directory where your
        configuration.nix lives. It generates a tag for each sucessfull
        build of a system. So everytime you rebuild your system, this
        script creates a tag for you, so you can revert your
        configuration.nix to the generation state.

        Example usage:

            # To rebuild the system with "nixos-rebuild switch" (by -c
            # switch), tag in /home/me/config and include the hostname
            # in the tag as well (helpful if your configuration is
            # shared between hosts).
            # Verbosity is on here.
            nix-script -v switch -c switch -w /home/me/config -n

        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

        can be used to use your local clone of the nixpkgs repository.

$(help_rcvars                                                       \
    "RC_CONFIG  - Path of your system configuration (git) directory"\
    "RC_NIXPKGS - Path of your nixpkgs clone"                       \
    "RC_SWITCH_DEFAULT_TAG_FLAGS            - Default git-tag flags for tagging in system configuration (git) directory"\
    "RC_SWITCH_DEFAULT_TAG_FLAGS_NIXPKGS    - Default git-tag flags for tagging in nixpkgs"
)

$(help_end "${BASH_SOURCE[0]}")
EOS
}

COMMAND=
ARGS=
WD=$RC_CONFIG
TAG_NAME=
HOSTNAME="$(hostname)"
TAG_NIXPKGS=0
NIXPKGS=$RC_NIXPKGS
TAG_FLAGS="$RC_SWITCH_DEFAULT_TAG_FLAGS"
TAG_FLAGS_NIXPKGS="$RC_SWITCH_DEFAULT_TAG_FLAGS_NIXPKGS"
DONT_BUILD=
QUIET=1

while getopts "c:w:t:nbp:f:qh" OPTION
do
    case $OPTION in
        c)
            COMMAND=$OPTARG
            ;;

        w)
            WD=$OPTARG
            ;;

        t)
            TAG_NAME=$OPTARG
            ;;

        n)
            HOSTNAME=""
            ;;

        p)
            if [[ ! -z "$OPTARG" ]]
            then
                NIXPKGS=$OPTARG
            fi
            TAG_NIXPKGS=1
            ;;

        f)
            TAG_FLAGS=$OPTARG
            ;;

        b)
            DONT_BUILD=1
            dbg "DONT_BUILD = $DONT_BUILD"
            ;;

        q)
            QUIET=0
            dbg "QUIET = $QUIET"
            ;;

        h)
            usage
            exit 1
            ;;
    esac
done

#
# Function to generate the tag at $NIXPKGS as well
#
tag_nixpkgs() {
    if [[ ! -d "$1" ]]
    then
        stderr "'$1' is not a directory, so can't be a nixpkgs clone"
        return
    fi

    commit=$(nixos-version | cut -d . -f 3 | cut -d " " -f 1)

    c_txt="Trying to create tag '$TAG_NAME' at '$1' on commit '$commit'"
    continue_question "$c_txt" || return

    __git "$1" tag $TAG_FLAGS_NIXPKGS "$TAG_NAME" $commit || \
        stderr "Could not create tag in nixpkgs clone"
}


dbg "COMMAND    = $COMMAND"
dbg "WD         = $WD"
dbg "TAG_NAME   = $TAG_NAME"
dbg "HOSTNAME   = $HOSTNAME"
dbg "NIXPKGS    = $NIXPKGS"
dbg "TAG_FLAGS  = $TAG_FLAGS"
ARGS=$(echo $* | sed -r 's/(.*)(\-\-(.*)|$)/\2/')
dbg "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"

if [[ -z "$DONT_BUILD" ]]
then
    __q="-Q"
    [[ $QUIET -eq 0 ]] && __q=""
    explain sudo nixos-rebuild $__q $COMMAND $ARGS
    REBUILD_EXIT=$?
else
    stdout "Do not call nixos-rebuild"
    REBUILD_EXIT=0
fi

if [[ $REBUILD_EXIT -eq 0 ]]
then
    LASTGEN=$(current_system_generation)
    sudo -k

    stdout "sudo -k succeeded"
    stdout "Last generation was: $LASTGEN"

    if [[ -z "$TAG_NAME" ]]
    then
        if [[ -z "$HOSTNAME" ]]; then TAG_NAME="nixos-$LASTGEN-$COMMAND"
        else TAG_NAME="nixos-$HOSTNAME-$LASTGEN-$COMMAND"
        fi
    fi

    __git "$WD" tag $TAG_FLAGS "$TAG_NAME"

    if [[ $TAG_NIXPKGS -eq 1 ]]
    then
        if [[ ! -z "$NIXPKGS" ]]
        then
            stdout "Trying to generate tag in $NIXPKGS"
            tag_nixpkgs "$NIXPKGS"
        else
            stderr "Do not generate a tag in the nixpkgs clon"
            stderr "no NIXPKGS given."
            usage
            stderr "Continuing..."
        fi
    else
        stdout "nixpkgs tag generating disabled"
    fi

else
    stderr "Switching failed. Won't executing any further commands."
    exit $REBUILD_EXIT
fi