-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup-home.sh
96 lines (80 loc) · 2.44 KB
/
setup-home.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
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
usage() { echo "Usage: $0 [-u <user>]" 1>&2; exit 1; }
homedir() {
local result; result="$(getent passwd "$1")" || (echo "User $1 does not exist"; exit 1)
echo "$result" | cut -d : -f 6
}
while getopts ":u:" o; do
case "${o}" in
u)
user=${OPTARG}
;;
*)
usage
;;
esac
done
shift $((OPTIND-1))
if [ -z "${user}" ] ; then
user="$(whoami)"
fi
home="$(homedir $user)"
nixpkgs="$home/.config/nixpkgs"
homemanager="$home/.config/home-manager"
nixhome_file="$homemanager/home.nix"
nixhome_dir="$homemanager/nix-home.d"
mkdir -p "$nixpkgs"
mkdir -p "$homemanger"
if ! [ -d "${nixhome_dir}" ]; then
echo "Unable to find extended configuration directory nix-home.d in ${nixpkgs}"
echo "Where did you get this script from?"
echo "Would you like to initialize nix-home.d from https://github.com/strangeglyph/nix-home now?"
read -p "y/N> " answer
if [ "${answer}" = "y" ]; then
git clone https://github.com/strangeglyph/nix-home "${nixhome_dir}" || (echo "Initialization failed"; exit 1)
else
echo "Please initialize nix-home.d manually"
exit 1
fi
fi
if [ -f "${nixhome_file}" ] ; then
echo "Backing up ${nixhome_file} to ${nixhome_file}.old"
mv "${nixhome_file}" "${nixhome_file}.old"
fi
echo "Writing new home.nix"
cat > "${nixhome_file}" <<EOF
# This file has been auto-generated. Please modify the default
# in ./nix-home.d/home/default.nix or the host specific configuration
# in ./nix-home.d/home/$(hostname)/default.nix or the user specific configuration
# in ./nix-home.d/home/$(hostname)/${user}.nix
{ config, pkgs, ... }:
{
imports =
[
./nix-home.d/home/default.nix
./nix-home.d/home/$(hostname)/default.nix
./nix-home.d/home/$(hostname)/${user}.nix
];
}
EOF
mkdir -p "${nixhome_dir}/home/$(hostname)"
if ! [ -f "${nixhome_dir}/home/$(hostname)/default.nix" ]; then
echo "$(hostname)/default.nix does not exist - initializing empty"
cat > "${nixhome_dir}/home/$(hostname)/default.nix" <<EOF
{ config, pkgs, ... }:
{
}
EOF
fi
if ! [ -f "${nixhome_dir}/home/$(hostname)/${user}.nix" ]; then
echo "$(hostname)/${user}.nix does not exist - initializing defaults"
cat > "${nixhome_dir}/home/$(hostname)/${user}.nix" <<EOF
{ config, pkgs, ... }:
{
home.username = "${user}";
home.homeDirectory = "${home}";
}
EOF
fi
if ! [ -d "$home/nix-home" ]; then
ln -s "$nixhome_dir" "$home/nix-home"
fi