-
Notifications
You must be signed in to change notification settings - Fork 160
/
Copy pathgen-grafana-creds.nix
36 lines (34 loc) · 1.26 KB
/
gen-grafana-creds.nix
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
{ user ? null, password ? null, pkgs ? import ../nix { } }:
pkgs.stdenv.mkDerivation {
name = "gen-grafana-creds";
buildInputs = with pkgs; [ pwgen ];
shellHook = ''
credsFilename="grafana-creds.nix" # Default grafana static filename
defaultUser="root" # Default administrative user
password="${toString password}" # password supplied by cli arg
passwordChar="32" # Default grafana password length
staticPath=${toString ../static} # Absolute path to the static dir
user="${toString user}" # user supplied by cli arg
if [[ -e "$staticPath/$credsFilename" ]]; then
echo "File already exists: $staticPath/$credsFilename, aborting!"
exit 1
elif [[ -z $user ]]; then
echo "User is empty -- setting to a default administrative user of $defaultUser"
user=$defaultUser
fi
echo "Writing grafana creds for user $user..."
if [[ -z $password ]]; then
echo "Password is empty -- setting to a random alphanumeric password of length $passwordChar"
password=$(pwgen -s $passwordChar 1)
fi
umask 077
cd $path
cat << EOF > $staticPath/$credsFilename
{
user = "$user";
password = "$password";
}
EOF
exit 0
'';
}