diff options
| author | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2026-11-02 14:01:09 +0100 |
|---|---|---|
| committer | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2026-11-02 14:01:09 +0100 |
| commit | 6e2d89c5e00a63699527853d15db2a73e33ec283 (patch) | |
| tree | 35f6cf78d10e2638b9ef24f9f4fb3d1f0e8dc8bc /modules | |
| parent | treewide: Apply changes from server/system config alignment (diff) | |
| download | nixos-config-6e2d89c5e00a63699527853d15db2a73e33ec283.zip | |
modules/nix: Add nix wrapper, that checks nix store size before working
Diffstat (limited to 'modules')
| -rw-r--r-- | modules/by-name/ni/nix/module.nix | 34 | ||||
| -rwxr-xr-x | modules/by-name/ni/nix/nix_wrapper.sh | 25 |
2 files changed, 58 insertions, 1 deletions
diff --git a/modules/by-name/ni/nix/module.nix b/modules/by-name/ni/nix/module.nix index b7e64989..e46d6e85 100644 --- a/modules/by-name/ni/nix/module.nix +++ b/modules/by-name/ni/nix/module.nix @@ -21,6 +21,38 @@ }: let inherit (sources) nixpkgs; + lix = + pkgs.lixPackageSets.latest.lix; + + nixWrapperBase = pkgs.writeShellApplication { + inherit (lix) name; + + text = builtins.readFile ./nix_wrapper.sh; + inheritPath = false; + runtimeInputs = [ + pkgs.lixPackageSets.latest.lix + pkgs.libnotify + pkgs.btrfs-progs + pkgs.coreutils + pkgs.gnused + pkgs.gnugrep + pkgs.bc + ]; + + derivationArgs = { + inherit (lix) meta pname version; + }; + }; + + nixWrapper = pkgs.symlinkJoin { + inherit (lix) name; + paths = [ + nixWrapperBase + pkgs.lixPackageSets.latest.lix + ]; + inherit (lix) meta pname version; + }; + cfg = config.soispha.nix; in { options.soispha.nix = { @@ -29,7 +61,7 @@ in { config = lib.mkIf cfg.enable { nix = { - package = pkgs.lixPackageSets.latest.lix; + package = nixWrapper; # Disable nix channels (this is a remnant of old days) channel.enable = false; diff --git a/modules/by-name/ni/nix/nix_wrapper.sh b/modules/by-name/ni/nix/nix_wrapper.sh new file mode 100755 index 00000000..55b12dc9 --- /dev/null +++ b/modules/by-name/ni/nix/nix_wrapper.sh @@ -0,0 +1,25 @@ +#! /usr/bin/env sh + +set -eu + +notify-send "Running nix via the wrapper." + +if [ "$_NIX_WRAPPER_FALLBACK" = "true" ]; then + # Allow skipping the size check. + nix "$@" +else + usage="$(btrfs filesystem usage /nix --raw 2>/dev/null)" + avail="$(echo "$usage" | grep 'Device size:' | cut -f 3 | sed 's/ //g')" + used="$(echo "$usage" | grep 'Used:' | cut -f 4 | head -n 1 | sed 's/ //g')" + perc="$(printf "scale=3\n%d / %d\n" "$used" "$avail" | bc)" + perc="$(printf "%d * 100\n" "$perc" | bc)" + + if test -gt "$perc" "0.2"; then + nix "$@" + else + echo "Not enough space free, in nix store: Only, $perc is still free." + exit 3 + fi +fi + +# vim: ft=sh |
