Repository navigation
Expand file tree
/
Copy pathbootstrap-nix
More file actions
111 lines (89 loc) · 2.88 KB
/
Copy pathbootstrap-nix
File metadata and controls
111 lines (89 loc) · 2.88 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
111
#!/usr/bin/env bash
# Config
HOME=$(realpath "$HOME") # Yeah, I don't know why argon does this
nix_version="2.23"
nix_url="https://hydra.nixos.org/job/nix/maintenance-${nix_version}/buildStatic.x86_64-linux/latest/download-by-type/file/binary-dist"
nix_location="$HOME/.local/bin"
bash_profile="$HOME/.bashrc"
bash_dir="$HOME/.bashrc.d"
bashrc_fragment="$bash_dir/nix.bash"
nix_exe="${nix_location}/nix"
nix_conf_home="${XDG_CONFIG_HOME:-$HOME/.config}"/nix
# Run
cat << EOF
This script will install nix locally, it was designed to be minimally invasive.
It will, however, make a couple of changes to your environment.
1) It will download the official nix executeable into $nix_location
2) It will modify your bashrc $bash_profile so nix is discoverable
3) It will add git to your default module set
4) It will add a monthly cron job to cleanup leftover nix data
EOF
read -p "Would you like to continue (y/n)? " -n 1 -r choice
case "$choice" in
y|Y ) ;;
* ) echo "Aborting" && exit 1;;
esac
echo
# Download nix
download_nix() {
nix_exe=$(realpath "$nix_exe")
mkdir -p "$nix_location"
echo "Downloading nix to $nix_exe"
curl -L "$nix_url" -o "$nix_exe"
chmod +x "$nix_exe"
}
download_nix
# Modify bashrc
echo "Modifying bashrc"
mkdir -p "$bash_dir"
cat << 'EOF' > "$bashrc_fragment"
# This will add a directory to a path if its not already there
pathadd() {
if [ -d "$1" ] && [[ ":$PATH:" != *":$1:"* ]]; then
PATH="${PATH:+"$PATH:"}$1"
fi
}
# Home should not be a symlink, I'm not really sure why it is one
HOME=$(realpath "$HOME")
# Nix needs to know where this is
export NIX_SSL_CERT_FILE=/etc/ssl/certs/ca-bundle.crt
# Add .local/bin to the path
pathadd "$HOME/.local/bin"
EOF
grep -q "$bashrc_fragment" "$bash_profile" || {
cat <<- EOF >> "$bash_profile"
# Load nix
. "$bashrc_fragment"
EOF
}
# Modify module set
echo "Loading git module"
module load git
echo "Saving current module set"
module save
# Configure nix
mkdir -p "$nix_conf_home"
nix_conf="$nix_conf_home"/nix.conf
grep -q "experimental-features" "$nix_conf" || cat "experimental-features = nix-command flakes" >> "$nix_conf"
grep -q "sandbox = false" "$nix_conf" || cat "sandbox = false" >> "$nix_conf"
link_to_nix() {
ln -s "${nix_location}/nix" "${nix_location}/${1}"
}
link_to_nix "nix-build"
link_to_nix "nix-channel"
link_to_nix "nix-collect-garbage"
link_to_nix "nix-copy-closure"
link_to_nix "nix-env"
link_to_nix "nix-hash"
link_to_nix "nix-instantiate"
link_to_nix "nix-prefetch-url"
link_to_nix "nix-shell"
link_to_nix "nix-store"
# Setup cronjob
echo "Configuring cronjob"
cron_cmd="HOME=$HOME $HOME/.local/bin/nix-collect-garbage"
cron_job="@monthly $cron_cmd"
( crontab -l | grep -v -F "$cron_cmd" || : ; echo "$cron_job") | crontab -
# Check
echo "Initial run of nix to make sure everthing works, this may take a second..."
$nix_exe run nixpkgs#cowsay -- "All done! You probably have to re-login or run \`. ~/.bashrc\`"