summary refs log tree commit diff
path: root/maintainers/scripts/nix-diff.sh
blob: 0c65e29cf4351210fc07e35e147a503ee223d053 (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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/usr/bin/env nix-shell
#! nix-shell -i bash -p coreutils gnugrep gnused

################################################################################
# nix-diff.sh                                                                  #
################################################################################
# This script "diffs" Nix profile generations.                                 #
#                                                                              #
# Example:                                                                     #
################################################################################
# > nix-diff.sh 90 92                                                          #
# + gnumake-4.2.1                                                              #
# + gnumake-4.2.1-doc                                                          #
# - htmldoc-1.8.29                                                             #
################################################################################
# The example shows that as of generation 92 and since generation 90,          #
# gnumake-4.2.1 and gnumake-4.2.1-doc have been installed, while               #
# htmldoc-1.8.29 has been removed.                                             #
#                                                                              #
# The example above shows the default, minimal output mode of this script.     #
# For more features, run `nix-diff.sh -h` for usage instructions.              #
################################################################################

usage() {
    cat <<EOF
usage: nix-diff.sh [-h | [-p profile | -s] [-q] [-l] [range]]
-h:         print this message before exiting
-q:         list the derivations installed in the parent generation
-l:         diff every available intermediate generation between parent and
            child
-p profile: specify the Nix profile to use
            * defaults to ~/.nix-profile
-s:         use the system profile
            * equivalent to: -p /nix/var/nix/profiles/system
profile:    * should be something like /nix/var/nix/profiles/default, not a
              generation link like /nix/var/nix/profiles/default-2-link
range:      the range of generations to diff
            * the following patterns are allowed, where A, B, and N are positive
              integers, and G is the currently active generation:
                A..B => diffs from generation A to generation B
                ~N   => diffs from the Nth newest generation (older than G) to G
                A    => diffs from generation A to G
            * defaults to ~1
EOF
}

usage_tip() {
    echo 'run `nix-diff.sh -h` for usage instructions' >&2
    exit 1
}

while getopts :hqlp:s opt; do
    case $opt in
        h)
            usage
            exit
            ;;
        q)
            opt_query=1
            ;;
        l)
            opt_log=1
            ;;
        p)
            opt_profile=$OPTARG
            ;;
        s)
            opt_profile=/nix/var/nix/profiles/system
            ;;
        \?)
            echo "error: invalid option -$OPTARG" >&2
            usage_tip
            ;;
    esac
done
shift $((OPTIND-1))

if [ -n "$opt_profile" ]; then
    if ! [ -L "$opt_profile" ]; then
        echo "error: expecting \`$opt_profile\` to be a symbolic link" >&2
        usage_tip
    fi
else
    opt_profile=$(readlink ~/.nix-profile)
    if (( $? != 0 )); then
        echo 'error: unable to dereference `~/.nix-profile`' >&2
        echo 'specify the profile manually with the `-p` flag' >&2
        usage_tip
    fi
fi

list_gens() {
    nix-env -p "$opt_profile" --list-generations \
        | sed -r 's:^\s*::' \
        | cut -d' ' -f1
}

current_gen() {
    nix-env -p "$opt_profile" --list-generations \
        | grep -E '\(current\)\s*$' \
        | sed -r 's:^\s*::' \
        | cut -d' ' -f1
}

neg_gen() {
    local i=0 from=$1 n=$2 tmp
    for gen in $(list_gens | sort -rn); do
        if ((gen < from)); then
            tmp=$gen
            ((i++))
            ((i == n)) && break
        fi
    done
    if ((i < n)); then
        echo -n "error: there aren't $n generation(s) older than" >&2
        echo " generation $from" >&2
        return 1
    fi
    echo $tmp
}

match() {
    argv=("$@")
    for i in $(seq $(($#-1))); do
        if grep -E "^${argv[$i]}\$" <(echo "$1") >/dev/null; then
            echo $i
            return
        fi
    done
    echo 0
}

case $(match "$1" '' '[0-9]+' '[0-9]+\.\.[0-9]+' '~[0-9]+') in
    1)
        diffTo=$(current_gen)
        diffFrom=$(neg_gen $diffTo 1)
        (($? == 1)) && usage_tip
        ;;
    2)
        diffFrom=$1
        diffTo=$(current_gen)
        ;;
    3)
        diffFrom=${1%%.*}
        diffTo=${1##*.}
        ;;
    4)
        diffTo=$(current_gen)
        diffFrom=$(neg_gen $diffTo ${1#*~})
        (($? == 1)) && usage_tip
        ;;
    0)
        echo 'error: invalid invocation' >&2
        usage_tip
        ;;
esac

dirA="${opt_profile}-${diffFrom}-link"
dirB="${opt_profile}-${diffTo}-link"

declare -a temp_files
temp_length() {
    echo -n ${#temp_files[@]}
}
temp_make() {
    temp_files[$(temp_length)]=$(mktemp)
}
temp_clean() {
    rm -f ${temp_files[@]}
}
temp_name() {
    echo -n "${temp_files[$(($(temp_length)-1))]}"
}
trap 'temp_clean' EXIT

temp_make
versA=$(temp_name)
refs=$(nix-store -q --references "$dirA")
(( $? != 0 )) && exit 1
echo "$refs" \
    | grep -v env-manifest.nix \
    | sort \
          > "$versA"

print_tag() {
    local gen=$1
    nix-env -p "$opt_profile" --list-generations \
        | grep -E "^\s*${gen}" \
        | sed -r 's:^\s*::' \
        | sed -r 's:\s*$::'
}

if [ -n "$opt_query" ]; then
    print_tag $diffFrom
    cat "$versA" \
        | sed -r 's:^[^-]+-(.*)$:    \1:'

    print_line=1
fi

if [ -n "$opt_log" ]; then
    gens=$(for gen in $(list_gens); do
               ((diffFrom < gen && gen < diffTo)) && echo $gen
           done)
    # Force the $diffTo generation to be included in this list, instead of using
    # `gen <= diffTo` in the preceding loop, so we encounter an error upon the
    # event of its nonexistence.
    gens=$(echo "$gens"
           echo $diffTo)
else
    gens=$diffTo
fi

temp_make
add=$(temp_name)
temp_make
rem=$(temp_name)
temp_make
out=$(temp_name)

for gen in $gens; do

    [ -n "$print_line" ] && echo

    temp_make
    versB=$(temp_name)

    dirB="${opt_profile}-${gen}-link"
    refs=$(nix-store -q --references "$dirB")
    (( $? != 0 )) && exit 1
    echo "$refs" \
        | grep -v env-manifest.nix \
        | sort \
              > "$versB"

    in=$(comm -3 -1 "$versA" "$versB")
    sed -r 's:^[^-]*-(.*)$:\1+:' <(echo "$in") \
        | sort -f \
               > "$add"

    un=$(comm -3 -2 "$versA" "$versB")
    sed -r 's:^[^-]*-(.*)$:\1-:' <(echo "$un") \
        | sort -f \
               > "$rem"

    cat "$rem" "$add" \
        | sort -f \
        | sed -r 's:(.*)-$:- \1:' \
        | sed -r 's:(.*)\+$:\+ \1:' \
        | grep -v '^$' \
              > "$out"

    if [ -n "$opt_query" -o -n "$opt_log" ]; then

        lines=$(wc -l "$out" | cut -d' ' -f1)
        tag=$(print_tag "$gen")
        (( $? != 0 )) && exit 1
        if [ $lines -eq 0 ]; then
            echo "$tag   (no change)"
        else
            echo "$tag"
        fi
        cat "$out" \
            | sed 's:^:    :'

        print_line=1

    else
        echo "diffing from generation $diffFrom to $diffTo"
        cat "$out"
    fi

    versA=$versB

done

exit 0