about summary refs log tree commit diff
diff options
context:
space:
mode:
authorPatryk Niedzwiedzinski <patryk@niedzwiedzinski.cyou>2024-09-29 16:52:06 +0200
committerPatryk Niedzwiedzinski <patryk@niedzwiedzinski.cyou>2024-09-29 16:52:06 +0200
commit5ec63e54cf7ef60e1a93db05d7f21db87bb5eeff (patch)
tree46a199965f1be93ac8c41d0f0c9399d1d392d94c
parentd66515907f2902edecfc42cd2c387de75b6dcfc4 (diff)
downloaddots-5ec63e54cf7ef60e1a93db05d7f21db87bb5eeff.tar.gz
dots-5ec63e54cf7ef60e1a93db05d7f21db87bb5eeff.zip
Add rebuild
-rw-r--r--rebuild/rebuild.nix5
-rwxr-xr-xrebuild/rebuild.sh49
2 files changed, 54 insertions, 0 deletions
diff --git a/rebuild/rebuild.nix b/rebuild/rebuild.nix
new file mode 100644
index 0000000..f4b5e39
--- /dev/null
+++ b/rebuild/rebuild.nix
@@ -0,0 +1,5 @@
+{ stdenv, pkgs }:
+pkgs.runCommand "rebuild" ''
+  mkdir -p $out/bin
+  cp ${./rebuild.sh} $out/bin/rebuild
+'';
diff --git a/rebuild/rebuild.sh b/rebuild/rebuild.sh
new file mode 100755
index 0000000..1c6c1bc
--- /dev/null
+++ b/rebuild/rebuild.sh
@@ -0,0 +1,49 @@
+#!/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
+
+# A rebuild script that commits on a successful build
+set -e
+
+# cd to your config dir
+pushd $DIR
+
+# Early return if no changes were detected (thanks @singiamtel!)
+if git diff --quiet '*'; then
+    echo "No changes detected, exiting."
+    popd
+    exit 0
+fi
+
+# Autoformat your nix files
+# alejandra . &>/dev/null \
+#  || ( alejandra . ; echo "formatting failed!" && exit 1)
+
+# Shows your changes
+git diff -U0 '*.nix'
+
+echo "NixOS Rebuilding..."
+
+# Rebuild, output simplified errors, log trackebacks
+sudo nixos-rebuild switch --flake $DIR/flake.nix &>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
+git commit -am "$current"
+
+# Back to where you were
+popd
+
+# Notify all OK!
+notify-send -e "NixOS Rebuilt OK!" --icon=software-update-available