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
|
#!/usr/bin/env bash
#
# I believe there are a few ways to do this:
#
# 1. My current way, using a minimal /etc/nixos/configuration.nix that just imports my config from my home directory (see it in the gist)
# 2. Symlinking to your own configuration.nix in your home directory (I think I tried and abandoned this and links made relative paths weird)
# 3. My new favourite way: as @clot27 says, you can provide nixos-rebuild with a path to the config, allowing it to be entirely inside your dotfies, with zero bootstrapping of files required.
# `nixos-rebuild switch -I nixos-config=path/to/configuration.nix`
# 4. If you uses a flake as your primary config, you can specify a path to `configuration.nix` in it and then `nixos-rebuild switch —flake` path/to/directory
# As I hope was clear from the video, I am new to nixos, and there may be other, better, options, in which case I'd love to know them! (I'll update the gist if so)
DIR=$HOME/nixos
OPT=$1
FORCE=false
[ "$1" = "-f" ] && FORCE=true
# A rebuild script that commits on a successful build
set -e pipefail
# cd to your config dir
pushd $DIR
# Early return if no changes were detected (thanks @singiamtel!)
if $FORCE; then
echo Forcing rebuild
else
if git diff --quiet '*'; then
echo "No changes detected, exiting."
popd
exit 0
fi
fi
# Autoformat your nix files
# alejandra . &>/dev/null \
# || ( alejandra . ; echo "formatting failed!" && exit 1)
# Shows your changes
git diff -U0 '*'
echo "NixOS Rebuilding..."
# Rebuild, output simplified errors, log trackebacks
sudo nixos-rebuild switch --flake $DIR 2>&1 | tee nixos-switch.log
cat nixos-switch.log | grep --color error && exit 1
# Get current generation metadata
current=$(nixos-rebuild list-generations | grep current)
# Commit all changes witih the generation metadata
if $FORCE; then
exit 0
fi
git commit -am "$(hostname): $current"
# Back to where you were
popd
# Notify all OK!
notify-send -e "NixOS Rebuilt OK!" --icon=software-update-available
|