From ec5ff115cebf53532d93ec60e838f013403d8b13 Mon Sep 17 00:00:00 2001 From: John Lancaster <32917998+jsl12@users.noreply.github.com> Date: Sun, 12 Apr 2026 11:00:03 -0500 Subject: [PATCH] writeShellApplication reworks --- modules/nix-tools/rebuild.nix | 143 ++++++++++++++++++++-------------- 1 file changed, 85 insertions(+), 58 deletions(-) diff --git a/modules/nix-tools/rebuild.nix b/modules/nix-tools/rebuild.nix index 8d2b195..1edbdbe 100644 --- a/modules/nix-tools/rebuild.nix +++ b/modules/nix-tools/rebuild.nix @@ -6,11 +6,21 @@ flakeDir = config.rebuild.flakeDir; echoCmd = lib.getExe' pkgs.coreutils "echo"; hostnameCmd = "$(${lib.getExe pkgs.hostname} -s)"; - nfs = (pkgs.writeShellScriptBin "nfs" '' - HOSTNAME=${hostnameCmd} - ${echoCmd} "Switching to the $HOSTNAME nixos profile" - sudo ${lib.getExe pkgs.nixos-rebuild} switch --impure --flake ${flakeDir}#$HOSTNAME - ''); + + nfs = with pkgs; writeShellApplication { + name = "nfs"; + runtimeInputs = [ coreutils hostname nh ]; + text = '' + HOSTNAME=$(hostname -s) + echo "Switching to the $HOSTNAME nixos profile" + sudo nh os switch "$@" ${flakeDir}#$HOSTNAME + ''; + }; + nfsu = with pkgs; writeShellApplication { + name = "nfsu"; + runtimeInputs = [ nfs pkgs.git ]; + text = ''nfs --refresh "$@"''; + }; in { options.rebuild = { @@ -22,29 +32,73 @@ }; config = { - environment.systemPackages = with pkgs; [ - nfs - (writeShellScriptBin "nfsu" '' - ${lib.getExe nix} flake update --impure --flake ${flakeDir} - ${lib.getExe git} -C ${flakeDir} add ${flakeDir}/flake.lock > /dev/null 2>&1 - ${lib.getExe nfs} - '') - ]; + environment.systemPackages = with pkgs; [ nfs nfsu ]; }; }; flake.modules.homeManager.rebuild = { config, pkgs, lib, ... }: let - nixBin = lib.getExe pkgs.nix; flakeDir = config.homeManagerFlakeDir; - echoCmd = lib.getExe' pkgs.coreutils "echo"; hostnameCmd = "$(${lib.getExe pkgs.hostname} -s)"; - nhms = (pkgs.writeShellScriptBin "nhms" '' - HOSTNAME=${hostnameCmd} - ${echoCmd} "Switching to the $HOSTNAME home-manager profile" - ${lib.getExe pkgs.nh} home switch ${flakeDir} -c $HOSTNAME "$@" - ''); + + flake-parts-check = with pkgs; writeShellApplication { + name = "flake-parts-check"; + runtimeInputs = [ nix ]; + text = '' + cd ${flakeDir} + nix run "${flakeDir}#write-flake" + nix flake check + ''; + }; + + nhms = with pkgs; writeShellApplication { + name = "nhms"; + runtimeInputs = [ coreutils hostname nh ]; + text = '' + HOSTNAME=$(hostname -s) + echo "Switching to the $HOSTNAME home-manager profile" + nh home switch ${flakeDir} -c "$HOSTNAME" "$@" + ''; + }; + + nhmu = with pkgs; writeShellApplication { + name = "nhmu"; + runtimeInputs = [ nhms ]; + text = ''nhms --update''; + }; + + test-build = with pkgs; writeShellApplication { + name = "test-build"; + runtimeInputs = [ coreutils nix hostname ]; + text = '' + if [ -z "$1" ]; then + HOSTNAME=$(hostname -s) + else + HOSTNAME="$1" + fi + echo "Testing the evaulation of the nixos config for $HOSTNAME" + nix eval "${flakeDir}#nixosConfigurations.$HOSTNAME.config.system.build.toplevel.drvPath" + ''; + }; + + cleanup = with pkgs; writeShellApplication { + name = "cleanup"; + runtimeInputs = [ coreutils home-manager nix ]; + text = '' + set -e + DAYS=$1 + if [ -z "$DAYS" ]; then + echo "usage: cleanup " + exit 1 + fi + home-manager expire-generations "-$DAYS days" + nix profile wipe-history --older-than "''${DAYS}d" + nix store gc + nix store optimise + ''; + }; + in { options = { @@ -53,52 +107,25 @@ type = lib.types.str; default = "${config.xdg.configHome}/home-manager"; }; - buildHostname = lib.mkOption { - description = "Hostname for the NixOS configuration to use."; - type = lib.types.str; - default = hostnameCmd; - }; }; config = { home.activation.printFlakeDir = lib.hm.dag.entryAfter ["writeBoundary"] '' - run ${echoCmd} "Home Manager flake directory: ${flakeDir}" + run echo "Home Manager flake directory: ${flakeDir}" ''; home.packages = with pkgs; [ home-manager - (writeShellScriptBin "flake-parts-check" '' - cd ${flakeDir} - ${nixBin} run "${flakeDir}#write-flake" - ${nixBin} flake check - '') - nhms - (writeShellScriptBin "nhmu" '' - ${lib.getExe nhms} --update - '') - - (writeShellScriptBin "test-build" '' - if [ -z "$1" ]; then - HOSTNAME=${hostnameCmd} - else - HOSTNAME="$1" - fi - ${echoCmd} "Testing the evaulation of the nixos config for $HOSTNAME" - ${lib.getExe nix} eval ${flakeDir}#nixosConfigurations.$HOSTNAME.config.system.build.toplevel.drvPath - '') - - (writeShellScriptBin "cleanup" '' - set -e - DAYS=$1 - if [ -z "$DAYS" ]; then - ${echoCmd} "usage: cleanup " - exit 1 - fi - ${lib.getExe home-manager} expire-generations "-$DAYS days" - ${lib.getExe nix} profile wipe-history --older-than "''${DAYS}d" - ${lib.getExe nix} store gc - ${lib.getExe nix} store optimise - '') + (symlinkJoin { + name = "build-tools"; + paths = [ + flake-parts-check + nhms + nhmu + test-build + cleanup + ]; + }) ]; }; };