summaryrefslogtreecommitdiffstats
path: root/nix-script-show-generation.sh
blob: 75bfda0e557e3e2870dcee8c7422180a23c25478 (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
#!/usr/bin/env bash

source $(dirname ${BASH_SOURCE[0]})/nix-utils.sh

usage() {
    cat <<EOS
    $(help_synopsis "${BASH_SOURCE[0]}" "[-s | -u | -p <profile>] [-h]")

        -s           | Show system generation
        -u           | Show user generation (default)
        -p <profile> | Show <profile> generation
        -h           | Show this help and exit

    Show the number of the current generations. Defaults to user
    profile, but system profile can be checked as well.

    Example usage:

        # To show the system generation which is current.
        # With verbosity on.
        nix-script -v show-generations -s

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

SYSTEM=0
USER=1
PROFILE=""

while getopts "sup:h" OPTION
do
    case $OPTION in
        s)
            SYSTEM=1
            USER=0
            stdout "Showing system generation"
            ;;
        u)
            SYSTEM=0
            USER=1
            stdout "Showing user generation"
            ;;
        p)
            SYSTEM=0
            USER=0
            PROFILE=$OPTARG
            dbg "PROFILE = $PROFILE"
            ;;

        h)
            stdout "Showing usage"
            usage
            exit 0
            ;;
    esac
done

if [[ ! -z "$PROFILE" ]]
then
    grep_generation "sudo nix-env -p /nix/var/nix/profiles/$PROFILE --list-generations"
else
    ([[ $SYSTEM -eq 1 ]] && current_system_generation) || \
        current_user_generation
fi


stdout "Ready"