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

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

#
# Do something explicitely without verbosity output
#
__quiet__() {
    VERBOSE=0 $*
}

#
# Do something explicitely without debug and verbosity output (with no output at
# all therefor)
#
__silent__() {
    DEBUG=0 __quiet__ $*
}

#
# Check whether a string (2nd arg) contains a substring (1st arg)
#
stringcontains() {
    [ -z "${2##*$1*}" ]
}

#
# Print on stderr, in red
#
stderr() {
    echo -e "${Red}[$(basename $0)]: ${*}${Color_Off}" >&2
}

#
# Print debugging output on stderr, in green
#
dbg() {
    [[ $DEBUG -eq 1 ]] && \
        echo -e "${Green}[DEBUG][$(basename $0)]: ${*}${Color_Off}" >&2
}

#
# Print on stdout, if verbosity is enabled, prefix in green
#
stdout() {
    [[ $VERBOSE -eq 1 ]] && echo -e "${Green}[$(basename $0)]:${Color_Off} $*"
}

#
# List all available commands as script path
#
all_commands() {
    find $(dirname ${BASH_SOURCE[0]}) \
        -type f \
        -executable \
        -name "nix-script-*.sh"
}

#
# Get the name of the script file for the command passed as argument
#
# Does not check whether the file exists.
#
script_for() {
    echo "$(dirname ${BASH_SOURCE[0]})/nix-script-${1}.sh"
}

#
# Get the command name from a script path
#
scriptname_to_command() {
    callee=$([ -z "$2" ] && echo "nix-script" || echo "$2")
    echo "$1" | sed -r "s,$(dirname ${BASH_SOURCE[0]})/$callee-(.*)\.sh$,\1,"
}

#
# Generate a help synopsis text
#
help_synopsis() {
    SCRIPT=$(scriptname_to_command $1); shift
    echo "usage: nix-script [-v] $SCRIPT $*"
}

#
# Helper for section on variables from the RC file for the script
#
help_rcvars() {
    echo -e "\tUsed nix-script.rc variables:"
    echo -e "\t-----------------------------"
    echo -e ""
    for s; do echo -e "\t\t${s}"; done
    echo -e ""
}

#
# generate a help text footnote
#
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 the next command
#
explain() {
    stdout "$*"
    $*
}

#
# Helper for greping the current generation
#
grep_generation() {
    $* | grep current | sed -r 's,\s*([0-9]*)(.*),\1,'
}

#
# get the current system generation
#
current_system_generation() {
    grep_generation "sudo nix-env -p /nix/var/nix/profiles/system --list-generations"
}

#
# get the current user generation
#
current_user_generation() {
    grep_generation "nix-env --list-generations"
}

#
# get the current channel generation
#
current_channel_generation() {
    grep_generation "sudo nix-env -p /nix/var/nix/profiles/per-user/root/channels --list-generations"
}

#
# Get the channel names
#
channel_names() {
    sudo nix-channel --list | cut -d " " -f 1
}

#
# Ask the user whether to continue or not
#
continue_question() {
	local answer
	echo -ne "${Yellow}$1 [yN]?:${Color_Off} " >&2
	read answer
		echo ""
	[[ "${answer}" =~ ^[Yy]$ ]] || return 1
}

#
# Ask whether a command should be executed or not.
#
ask_execute() {
    q="$1"; shift
	local answer
	echo -ne "${Yellow}$q${Color_Off} [Yn]? "
	read answer; echo
	[[ ! "${answer}" =~ ^[Nn]$ ]] && eval $*
}

#
# Helper for executing git commands in another git directory
#
__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
}

# Argument 1: Caller script name, format: "nix-script"
caller_util_all_commands() {
    find $(dirname ${BASH_SOURCE[0]}) -type f -name "${1}-*.sh"
}

# Argument 1: Caller script name, format: "nix-script"
caller_util_list_subcommands_for() {
    for cmd in $(caller_util_all_commands $1)
    do
        scriptname_to_command "$cmd" "$1"
    done | sort
}

# Argument 1: Caller script name
# Argzment 2: Command name
caller_util_script_for() {
    echo "$(dirname ${BASH_SOURCE[0]})/${1}-${2}.sh"
}

# Argument 1: Caller script name
# Argzment 2: Command name
caller_util_get_script() {
    local SCRIPT=$(caller_util_script_for $1 $2)

    [[ ! -f $SCRIPT ]] && stderr "Not available: $COMMAND -> $SCRIPT" && exit 1
    [[ ! -x $SCRIPT ]] && stderr "Not executeable: $SCRIPT" && exit 1

    echo "$SCRIPT"
}

#
# Container helper functions
#

# get the configuration.nix path for the container by name
container_conf_path() {
    echo /var/lib/containers/$1/etc/nixos/configuration.nix
}