New rebuild mechanism

Put most of it as a flake app, so we can mess with it without relying on
`rb` being rebuilt. Also nom nom!
This commit is contained in:
Geoffrey Frogeye 2024-06-10 02:11:04 +02:00
parent 7b9d9053bf
commit 17f0ba3370
Signed by: geoffrey
GPG key ID: C72403E7F82E6AD8
8 changed files with 115 additions and 85 deletions

View file

@ -12,13 +12,14 @@ GET_INPUTS_CMD = [
]
def process_flake(flake: str) -> None:
def process_flake(flakeUri: str) -> None:
# get full path
if not os.path.isfile(flake):
raise FileNotFoundError(f"Flake not found: {flake}")
dir = os.path.dirname(flake)
flakeUri = os.path.normpath(flakeUri)
flakeFile = os.path.join(flakeUri, "flake.nix")
if not os.path.isfile(flakeFile):
raise FileNotFoundError(f"Flake not found: {flakeUri}")
# import dependencies
p = subprocess.run(GET_INPUTS_CMD, cwd=dir, stdout=subprocess.PIPE)
p = subprocess.run(GET_INPUTS_CMD, cwd=flakeUri, stdout=subprocess.PIPE)
deps = json.loads(p.stdout)
p.check_returncode()
# for each dependency
@ -34,11 +35,8 @@ def process_flake(flake: str) -> None:
# get flake file corresponding
dep_path = dep_url.split(":")[1]
if not dep_path.startswith("/"):
dep_path = os.path.join(dir, dep_path)
dep_path = os.path.normpath(dep_path)
dep_flake = os.path.join(dep_path, "flake.nix")
# call this function with the flake file
process_flake(dep_flake)
dep_path = os.path.join(flakeUri, dep_path)
process_flake(dep_path)
# update lockfile
cmd = [
"nix",
@ -50,7 +48,7 @@ def process_flake(flake: str) -> None:
"update",
dep_name,
]
p = subprocess.run(cmd, cwd=dir)
p = subprocess.run(cmd, cwd=flakeUri)
p.check_returncode()
@ -59,6 +57,6 @@ if __name__ == "__main__":
description="Recursively update lockfiles "
"of flakes located on the system"
)
parser.add_argument("flake", help="Starting flake", default="flake.nix")
parser.add_argument("flake", help="Starting flake", default="/")
args = parser.parse_args()
process_flake(args.flake)

View file

@ -17,8 +17,13 @@ let
in
{
config = {
boot = {
# From nixos-generate-config
initrd.availableKernelModules = [ "xhci_pci" "ahci" "nvme" "usbhid" "sd_mod" "rtsx_usb_sdmmc" ];
kernelModules = [ "kvm-intel" ];
# UEFI works here, and variables can be touched
boot.loader = {
loader = {
efi.canTouchEfiVariables = lib.mkDefault true;
grub = {
enable = true;
@ -27,7 +32,11 @@ in
# TODO Maybe we could? In case the HDD doesn't boot anymore?
};
};
hardware.cpu.intel.updateMicrocode = true;
};
# Also from nixos-generate-config
hardware.enableRedistributableFirmware = true;
# TODO Do we really need that? Besides maybe microcode?
frogeye.desktop = {
x11_screens = [
displays.deskLeft.output

View file

@ -80,10 +80,6 @@
type = "app";
program = "${disko.packages.${system}.default}/bin/disko";
};
nixos-generate-config = {
type = "app";
program = "${pkgs.nixos-install-tools}/bin/nixos-generate-config";
};
nixos-install = {
type = "app";
program = "${pkgs.nixos-install-tools}/bin/nixos-install";
@ -98,6 +94,19 @@
${pkgs.lix}/bin/nix repl --expr 'let flake = builtins.getFlake "${self}"; in flake // flake.nixosConfigurations // rec { pkgs = import ${nixpkgs} {}; lib = pkgs.lib; }'
''}";
};
updateLocalFlakes = {
type = "app";
program = "${pkgs.writers.writePython3 "update-local-flakes" { }
(builtins.readFile ./common/update-local-flakes.py)}";
};
nixosRebuild = {
type = "app";
program = "${pkgs.writeShellScript "rebuild" ''${pkgs.writeShellApplication {
name = "rebuild";
runtimeInputs = with pkgs; [ nix-output-monitor nixos-rebuild ];
text = builtins.readFile ./os/rebuild.sh;
}}/bin/rebuild ${self} "$@"''}";
};
};
}
);

View file

@ -1,8 +1,4 @@
{ pkgs, config, ... }:
let
ulf = pkgs.writers.writePython3 "update-local-flakes" {
} (builtins.readFile ./update-local-flakes.py);
in
{
home.packages = [
(pkgs.writeShellApplication {
@ -14,32 +10,26 @@ in
verb="$1"
shift
fi
nixos_flake="/etc/nixos/flake.nix"
if [ -f "$nixos_flake" ]
nixos_flake="$(readlink -f /etc/nixos)"
if [ -f "$nixos_flake/flake.nix" ]
then
sudo ${ulf} "$nixos_flake"
# ${pkgs.nix-output-monitor}/bin/nom build "$(dirname "$nixos_flake")#nixosConfigurations.$HOSTNAME.config.system.build.toplevel"
if [ "$verb" = "switch" ] || [ "$verb" = "test" ]
then
sudo nixos-rebuild "$verb" --specialisation ${config.frogeye.polarity} "$@"
else
sudo nixos-rebuild "$verb" "$@"
fi
fi
hm_flake="${config.xdg.configHome}/home-manager/flake.nix"
if [ -f "$hm_flake" ]
then
${ulf} "$hm_flake"
home-manager "$verb" "$@"
fi
nod_flake="${config.xdg.configHome}/nix-on-droid/flake.nix"
if [ -f "$nod_flake" ]
then
${ulf} "$nod_flake"
nix-on-droid "$verb" --flake "$(dirname "$nod_flake")" "$@"
nix run "$nixos_flake#updateLocalFlakes" -- "$nixos_flake"
nix run "$nixos_flake#nixosRebuild" -- "$verb" "$@"
fi
# TODO Fix nix-on-droid and home-manager
# hm_flake="${config.xdg.configHome}/home-manager/flake.nix"
# if [ -f "$hm_flake" ]
# then
# {ulf} "$hm_flake"
# home-manager "$verb" "$@"
# fi
# nod_flake="${config.xdg.configHome}/nix-on-droid/flake.nix"
# if [ -f "$nod_flake" ]
# then
# {ulf} "$nod_flake"
# nix-on-droid "$verb" --flake "$(dirname "$nod_flake")" "$@"
# fi
'';
})
];
}
# TODO make it a flake application, optional nom (is slow), test then boot, flags to confirm each, nvd diff here

View file

@ -82,24 +82,9 @@ sudo nix --extra-experimental-features "nix-command flakes" run "${SCRIPT_DIR}#d
trap - ERR
rm "$luks_pass_file"
# Generate hardware-config.nix
sudo nix --extra-experimental-features "nix-command flakes" run "${SCRIPT_DIR}#nixos-generate-config" -- --no-filesystems --root "$mountpoint"
# --no-filesystems because they are imported via disko
sudo rm "$mountpoint/etc/nixos/configuration.nix"
# Plug system configuration into this git repo
sudo mkdir -p "${mountpoint}/etc/nixos"
echo "{
description = \"$name system config\";
inputs.entrypoint.url = \"git+file:$flake_uri\";
outputs = { self, entrypoint, ... }:
{
nixosConfigurations.$name = entrypoint.nixosConfigurations.$name.extendModules {
modules = [ ./hardware-configuration.nix ];
};
};
}" | sudo tee "${mountpoint}/etc/nixos/flake.nix" > /dev/null
# Everything there should be covered by (and conflicts with) the repo anyways.
# Save that system configuration uses this repo
sudo mkdir -p "${mountpoint}/etc"
sudo ln -sfn "${flake_uri}" "${mountpoint}/nixos"
# Install NixOS! Or create a new generation.
sudo nix --extra-experimental-features "nix-command flakes" run "${SCRIPT_DIR}#nixos-install" -- --no-root-password --root "$mountpoint" --flake "${mountpoint}/etc/nixos#${name}"

View file

@ -9,7 +9,6 @@
./common.nix
./desktop
./dev
./diff
disko.nixosModules.disko
./gaming
./geoffrey.nix

View file

@ -1,14 +0,0 @@
{ pkgs, lib, config, ... }:
{
config = {
system.activationScripts.diff = {
supportsDryActivation = true;
text = ''
if [ -h /run/current-system ]
then
${pkgs.nvd}/bin/nvd --nix-bin-dir=${pkgs.nix}/bin diff /run/current-system "$systemConfig"
fi
'';
};
};
}

54
os/rebuild.sh Normal file
View file

@ -0,0 +1,54 @@
# Handle arguments
self="$1"
verb="$2"
shift
shift
if [ "$verb" != "build" ] && [ "$verb" != "test" ] && [ "$verb" != "boot" ] && [ "$verb" != "switch" ] && [ "$verb" != "confirm" ]
then
echo "Action should be one of: build, test, boot, switch, confirm"
exit 2
fi
# Build, looking nice
tmpdir="$(mktemp -d)"
# sudo so the eval cache is shared with nixos-rebuild
sudo nom build "$self#nixosConfigurations.$HOSTNAME.config.system.build.toplevel" -o "$tmpdir/toplevel" "$@"
toplevel="$(readlink "$tmpdir/toplevel")"
rm -rf "$tmpdir"
# Show diff
nvd diff /nix/var/nix/profiles/system "$toplevel"
# Figure out specialisation
specialisationArgs=""
currentSystem="$(readlink /run/current-system)"
while read -r specialisation
do
if [ "$(readlink "/nix/var/nix/profiles/system/specialisation/$specialisation")" = "$currentSystem" ]
then
specialisationArgs=("--specialisation" "$specialisation")
fi
done <<< "$(ls /nix/var/nix/profiles/system/specialisation)"
# Apply
if [ "$verb" = "confirm" ]
then
echo "Apply configuration? [y/N]"
read -r confirm
fi
if [ "$verb" = "test" ] || [ "$verb" = "switch" ] || [ "$confirm" = "y" ]
then
sudo nixos-rebuild --flake "$self#$HOSTNAME" test "${specialisationArgs[@]}" "$@"
fi
# Set as boot
if [ "$verb" = "confirm" ]
then
echo "Set configuration as boot? [y/N]"
read -r confirm
fi
if [ "$verb" = "boot" ] || [ "$verb" = "switch" ] || [ "$confirm" = "y" ]
then
sudo nixos-rebuild --flake "$self#$HOSTNAME" boot "$@"
fi