summaryrefslogtreecommitdiffstats
path: root/nix-utils.sh
blob: 39c7b80a974c5ab1bfcc482642c65b1db7c71297 (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
#!/usr/bin/env bash

Color_Off='\e[0m'
Red='\e[0;31m'
Yellow='\e[0;33m'
Green='\e[0;32m'

stderr() {
    echo -e "${Red}[$(basename $0)]: ${*}${Color_Off}" >&2
}

stdout() {
    [[ $VERBOSE -eq 1 ]] && echo -e "${Green}[$(basename $0)]:${Color_Off} $*"
}

scriptname_to_command() {
        echo "$1" | sed 's,^\.\/nix-script-,,' | sed 's,\.sh$,,' | \
            sed -r "s,$(dirname ${BASH_SOURCE[0]})/nix-script-,,"
}

help_synopsis() {
    SCRIPT=$(scriptname_to_command $1); shift
    echo "usage: nix-script $SCRIPT $*"
}

help_end() {
    echo -e "\tAdding '-v' before the '$1' command turns on verbosity"
    echo -e ""
    echo -e "\tReleased under terms of GPLv2"
    echo -e "\t(c) 2015 Matthias Beyer"
    echo ""
}

explain() {
    stdout "$*"
    $*
}

grep_generation() {
    $* | grep current | sed -r 's,\s*([0-9]*)(.*),\1,'
}

current_system_generation() {
    grep_generation "sudo nix-env -p /nix/var/nix/profiles/system --list-generations"
}

current_user_generation() {
    grep_generation "nix-env --list-generations"
}

continue_question() {
	local answer
	echo -ne "${Yellow}$1 [yN]?:${Color_Off} " >&2
	read answer
		echo ""
	[[ "${answer}" =~ ^[Yy]$ ]] || return 1
}

ask_execute() {
    q="$1"; shift
	local answer
	echo -ne "${Yellow}$q${Color_Off} [Yn]? "
	read answer; echo
	[[ ! "${answer}" =~ ^[Nn]$ ]] && eval $*
}

__git() {
    DIR=$1; shift
    explain git --git-dir="$DIR/.git" --work-tree="$DIR" $*
}

# Gets the current branch name or the hash of the current rev if there is no
# branch
__git_current_branch() {
    branch_name=$(git symbolic-ref -q HEAD)
    branch_name=${branch_name##refs/heads/}
    ([[ -z "$branch_name" ]] && git rev-parse HEAD) || echo $branch_name
}