-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.sh
executable file
·64 lines (55 loc) · 1.74 KB
/
setup.sh
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
#!/usr/bin/env bash
TARGET_CONFIG=/etc/nixos/configuration.nix
DATE=$(date "+%F_%H-%M-%S")
BACKUP_CONFIG=/tmp/configuration_$DATE.nix
SCRIPT_DIR=$(dirname "$(readlink -f "$BASH_SOURCE")")
HOSTS_DIR=$SCRIPT_DIR/hosts
HOSTS=( $(ls $HOSTS_DIR | sed "s/\.nix//g") ) # strip .nix from end of filenames
usage() {
echo "usage: $0 HOST"
echo ""
echo "Generate configuration.nix and write it to $TARGET_CONFIG."
echo "If $TARGET_CONFIG already exists, then back it up to /tmp."
echo "The configuration will import:"
echo " - common/common.nix shared config, in this repo"
echo " - hosts/HOST.nix host config, in this repo"
echo " - hardware-configuration.nix platform config, generated by nixos-generate-config"
echo ""
echo "arguments:"
echo " HOST: the host to set up. Options: ${HOSTS[@]}"
}
# If exactly 1 argument is not provided
if [[ $# -ne 1 ]]; then
echo "No host provided."
usage
exit 1
fi
HOST=$1
# If $HOST is not in $HOSTS
if ! printf '%s\0' "${HOSTS[@]}" | grep -Fxqz -- $HOST; then
echo "Unknown host $HOST."
usage
exit 1
fi
# If configuration.nix exists, back it up to /tmp with a timestamp
if [[ -f $TARGET_CONFIG ]]; then
echo "Backing up existing $TARGET_CONFIG to $BACKUP_CONFIG"
cp $TARGET_CONFIG $BACKUP_CONFIG
fi
# Write the generated configuration
generate_config() {
echo "# Generated by `realpath $BASH_SOURCE`"
echo ""
echo "{ config, pkgs, ...}:"
echo ""
echo "{"
echo " imports ="
echo " ["
echo " ./hardware-configuration.nix"
echo " $SCRIPT_DIR/hosts/$HOST.nix"
echo " ];"
echo "}"
}
echo "Writing configuration to $TARGET_CONFIG"
echo ""
generate_config | sudo tee $TARGET_CONFIG