-
-
Save 0atman/1a5133b842f929ba4c1e195ee67599d5 to your computer and use it in GitHub Desktop.
{ | |
config, | |
pkgs, | |
options, | |
... | |
}: let | |
hostname = "oatman-pc"; # to alllow per-machine config | |
in { | |
networking.hostName = hostname; | |
imports = [ | |
/etc/nixos/hardware-configuration.nix | |
(/home/oatman/dotfiles/nixos + "/${hostname}.nix") | |
]; | |
} |
#!/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) | |
# A rebuild script that commits on a successful build | |
set -e | |
# Edit your config | |
$EDITOR configuration.nix | |
# cd to your config dir | |
pushd ~/dotfiles/nixos/ | |
# Early return if no changes were detected (thanks @singiamtel!) | |
if git diff --quiet '*.nix'; 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 &>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 |
oh of course! it's running in the shebang's process, not hijacking the outer shell 😅
I have a laptop with only two cores and - since everytime I started a rebuild it would freeze my desktop - I implemented the simplest throttling mechanism to tell nixos-rebuild to use only half of the available cores (snippet below)
# Detect processors
procs="$(nproc)"
if [ -z "${procs:-}" ]; then
echo "No processors detected! Assuming 2..."
procs="2"
fi
hprocs="$(( procs / 2 ))"
echo "Detected ${procs} processors, using ${hprocs} of them"
nixos-rebuild switch --max-jobs "${hprocs}" #...
I started with this script as a basis and now feel like giving back a little.
I am currently in the process of rewriting all my shellscripts in Rust for the superior error-handling and my ability to implement more complex logic and error handling, while being more maintainable than shellscripts.
For anyone interested, the (opinionated) rust version of this script is among my other dotscripts here:
https://github.com/AnyTimeTraveler/dotscripts/blob/52b8dab862ac2fc75d97abb607bf0be795371cbe/rust/nixos_rebuild/src/main.rs
For anyone that still uses Tris' script, recently the nixos-rebuild list-generations
(line 41 in the current 19th revision) format seems to have changed. It now produces a table, so the grep fails and the final current
string becomes an empty string, making the git commit fail.
The fix I used (needs jq
tool installed) was:
-current=$(nixos-rebuild list-generations | grep current)
+current=$(nixos-rebuild list-generations --json | jq '.[] | select (.current == true) | "\(.generation) \(.date) \(.nixosVersion) \(.kernelVersion)"')
The change above will print a commit message almost similar to the previous one before the format change.
@claudiofreitas well spotted! I don't think it's broken on my machine yet, but I MUCH prefer querying with the jq
!
I don't actually care about the date, nixosversion or kernel version, so my new line now:
nixos-rebuild list-generations --json | jq '.[] | select (.current == true) | "\(.generation)"'
While we're here, I've started to append the output from https://lib.rs/crates/lumen to make the output more useful
Something I would suggest doing is using the nh command as opposed to the default command, as it tells you what packages and services were added, what had it's version changed and what the old and new versions are, as well as what was removed. I also use the nix-output-monitor program which shows the dependency graph I screenshotted from my latest build.
EDIT: For specifics on the command I use when rebuilding, it's this,
nh os switch /home/raviex/.dotfiles --hostname Icy-Nix --update
which I have aliased to fr
in my terminal.
not for those (like me) that are using a classic non-flakes environment - you have to use nh v4, which is available in unstable.
@0atman As far I know
pushd
andpopd
act only inside of the shell that get's created when you execute the script, like if you had two terminals and you changed directory in one of them. Once the script has terminated it exits to the "outer shell" (bash or zsh) and resume the previous environment