-
Notifications
You must be signed in to change notification settings - Fork 2
/
setup-home
executable file
·120 lines (96 loc) · 3.84 KB
/
setup-home
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
112
113
114
115
116
117
118
119
120
#!/usr/bin/env bash
set -o errexit -o nounset -o xtrace
# TODO: Maybe modify to use the new `my-deploy-setup dir:$HOME` or `start0.sh` or `start1.bash`?
# This script sets-up the home directory of a newly-created user. It is
# intended to be run, by the new user, only once when the new home directory is
# empty. Normally, it is run with the current working directory being the new
# $HOME, but it could be run in any directory.
#
# E.g., after a new user has been added in /etc/nixos/configuration.nix, root
# should do:
# # passwd $NEW_USER
# # su -l $NEW_USER
# $ /etc/nixos/users/setup-home
# $ logout
function fail {
echo "Error:" "$@"
exit 1
} 1>&2
function assert-nonexistent {
local F="$DIR/$1"
[ ! -e "$F" ] || fail "$F already exists!"
}
function assert-all-nonexistent {
local F
while F="${1:-}"; shift; do
assert-nonexistent "$F"
done
}
function git-clone-into-nonempty {
local ARGS=("$@")
local REPO="${ARGS[-2]}"
local DEST="${ARGS[-1]}"
local OPTS=("${ARGS[@]:0:$#-2}")
mkdir cloned-"$DEST" # Empty
git clone "${OPTS[@]}" --no-checkout "$REPO" cloned-"$DEST"
mv cloned-"$DEST"/.git "$DEST"/
rmdir cloned-"$DEST"
(cd "$DEST"
git checkout)
}
DIR="${1:-.}"
DOTFILES_ORIGIN="${2:-/etc/nixos/users/dotfiles}"
cd "$DIR"
shopt -s nullglob dotglob
EXISTING_FILES=(*)
shopt -u nullglob dotglob
# Basic sanity check. Non-exhaustive since the checkout of the DOTFILES_ORIGIN
# will try to create other files that also should not exist yet.
assert-all-nonexistent .dotfiles .git .git-hidden .config/home-manager/home.nix
chmod -v og-rwx .
# Populate the user's home-directory with the system-wide dot-files "skeleton"
# by cloning the repository. Arrange for the Git directory to be .dotfiles
# instead of .git, to avoid seeing $DIR (e.g. $HOME) by default as a repository.
{
if [ ${#EXISTING_FILES[@]} -eq 0 ]; then
CLONE_COMMAND="git clone"
else
CLONE_COMMAND=git-clone-into-nonempty
fi
$CLONE_COMMAND --separate-git-dir=.dotfiles "$DOTFILES_ORIGIN" .
git checkout -b user/"$USER" --no-track
# Ensure that the local user name is always used for commits to a user's
# ~/.dotfiles repository:
git config user.name "$USER"
git config user.email "$USER@$HOSTNAME"
mv -v .git .git-hidden
}
# The .dotfiles repository provides .config/user-dirs.dirs but the directories
# which that declares do not exist yet, and the presence of that file would
# cause the xdg-user-dirs-update invocations at session logins to change the
# file to not declare non-existent directories, which is not what we want. To
# avoid that, invoke xdg-user-dirs-update once with the files being absent so it
# will create the default directories along with recreating the files, which is
# what we want and which matches the tracked content of those files.
{
rm -f .config/user-dirs.*
xdg-user-dirs-update
}
# Install home-manager per user and activate. The dot-files installed above
# provide a premade .config/home-manager/home.nix file.
{
[[ "$(nixos-version)" =~ ^([[:digit:]]+\.[[:digit:]]+) ]] # errexit on fail
NIXOS_RELEASE="${BASH_REMATCH[1]}"
HOMEMANAGER_URL="https://github.com/nix-community/home-manager/archive/release-$NIXOS_RELEASE.tar.gz"
nix-channel --add "$HOMEMANAGER_URL" home-manager
nix-channel --update
export NIX_PATH="$HOME/.nix-defexpr/channels${NIX_PATH:+:}$NIX_PATH"
# Ensure the state version that this Home Manager installation will be initialized with is the
# current latest.
STATE_VER_RE='home\.stateVersion *= *"[^"]+"'
STATE_VER_NEW="home.stateVersion = \"$NIXOS_RELEASE\""
sed -E -s -i -e "s/$STATE_VER_RE/$STATE_VER_NEW/" .config/home-manager/home.nix
nix-shell '<home-manager>' -A install
# Dump its news now so it will be quiet about this after.
home-manager news > /dev/null
}