Repository navigation
Expand file tree
/
Copy pathsetup.sh
More file actions
110 lines (87 loc) · 2.69 KB
/
Copy pathsetup.sh
File metadata and controls
110 lines (87 loc) · 2.69 KB
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
# shellcheck disable=SC1091,SC2148
source @bashLib@
export -f _log
### Gather flake info
readonly FLAKE_GIT_REPO="${1}"
nix_config="@flakePath@"
export nix_config
USER_NAME="$(logname)"
readonly USER_NAME
HOST_NAME="$(hostname)"
readonly HOST_NAME
_clone() {
local name="${1}"
local url="${2}"
local directory="${3}"
if [[ -d ${directory} ]]; then
_log "Not cloning ${name} because ${directory} already exists!"
return
fi
_log "Clone ${name}..."
su -c "git clone ${url} ${directory}" - "${USER_NAME}"
}
# clone repos
# shellcheck disable=SC2310
if ! _is_nixos || _is_root; then
_clone "nix-config" "${FLAKE_GIT_REPO}" "${nix_config}"
fi
# generage age key
_generate_age() {
local age_dir="${HOME}/.age"
local age_key_file="${age_dir}/key.txt"
if [[ ! -e "${HOME}/.age" ]]; then
mkdir -p "${age_dir}"
_log "Generating age key at ${age_key_file}"
local generated_age_key
generated_age_key="$(nix shell nixpkgs#age -c age-keygen -o "${age_key_file}" 2>&1)"
local age_pubkey_line
age_pubkey_line="$(sed -e "s,^Public key: \(.*\)\$,${HOST_NAME}-${USER} = \"${generated_age_key}\",")"
echo "${age_pubkey_line}" | tee -a "${nix_config}/.agenix.toml"
else
_log "Target directory ${age_dir} already exists, aborting age key generation!"
fi
}
export -f _generate_age
# installation
_setup_nixos() {
hostname=$(_read "Enter hostname" "${HOST_NAME}")
_generate_age
su "${USER_NAME}" -c "bash -c _generate_age"
_log "Linking flake to system config..."
ln -fs "${nix_config}/flake.nix" /etc/nixos/flake.nix
_log "Run nixos-rebuild for host ${hostname}..."
nixos-rebuild switch --keep-going --flake "${nix_config}#${hostname}"
}
_setup_nix() {
local nix_env_pkgs_json
nix_env_pkgs_json="$(nix-env -q --json)"
local nix_env_pkgs_names
nix_env_pkgs_names="$(echo "${nix_env_pkgs_json}" | jq ".[].pname")"
local has_nix_imperative=false
# preparation for non nixos systems
if echo "${nix_env_pkgs_names}" | grep -q '"nix"'; then
has_nix_imperative=true
_log "Set priority of installed nix package..."
nix-env --set-flag priority 1000 nix
fi
_generate_age
_log "Build home-manager activationPackage..."
nix build "${nix_config}#homeConfigurations.${USER_NAME}@${HOST_NAME}.activationPackage"
_log "Run activate script..."
HOME_MANAGER_BACKUP_EXT=hm-bak ./result/activate
rm -v result
# clean up
if ${has_nix_imperative}; then
_log "Uninstall manual installed nix package..."
nix-env --uninstall nix
fi
}
# shellcheck disable=SC2310
if _is_nixos && _is_root; then
_setup_nixos
elif ! _is_nixos && ! _is_root; then
_setup_nix
else
_log "You need to be root on NixOS or non-root on non-NixOS! Aborting..."
fi
echo