Files
dendritic/modules/nix-tools/rebuild.nix
T
2026-04-15 23:20:08 -05:00

133 lines
3.6 KiB
Nix

{ self, inputs, ... }:
{
flake.modules.nixos.rebuild =
{ config, pkgs, lib, ... }:
let
flakeDir = config.rebuild.flakeDir;
echoCmd = lib.getExe' pkgs.coreutils "echo";
hostnameCmd = "$(${lib.getExe pkgs.hostname} -s)";
nfs = with pkgs; writeShellApplication {
name = "nfs";
runtimeInputs = [ coreutils hostname nh ];
text = ''
HOSTNAME=$(hostname -s)
echo "Switching to the $HOSTNAME nixos profile"
nh os switch "$@" "${flakeDir}#$HOSTNAME"
'';
};
nfsu = with pkgs; writeShellApplication {
name = "nfsu";
runtimeInputs = [ nfs pkgs.git ];
text = ''nfs --refresh "$@"'';
};
in
{
options.rebuild = {
flakeDir = lib.mkOption {
description = "Path to the flake directory.";
type = lib.types.str;
default = "/etc/nixos";
};
};
config = {
environment.systemPackages = with pkgs; [ nfs nfsu ];
};
};
flake.modules.homeManager.rebuild =
{ config, pkgs, lib, ... }:
let
flakeDir = config.homeManagerFlakeDir;
hostnameCmd = "$(${lib.getExe pkgs.hostname} -s)";
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 <days>"
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 = {
homeManagerFlakeDir = lib.mkOption {
description = "Path to the home-manager flake directory.";
type = lib.types.str;
default = "${config.xdg.configHome}/home-manager";
};
};
config = {
home.activation.printFlakeDir = lib.hm.dag.entryAfter ["writeBoundary"] ''
run echo "Home Manager flake directory: ${flakeDir}"
'';
home.packages = with pkgs; [
home-manager
(symlinkJoin {
name = "build-tools";
paths = [
flake-parts-check
nhms
nhmu
test-build
cleanup
];
})
];
};
};
}