From 41ba46324ec0a611f6f6ec5358c35ceca144cddd Mon Sep 17 00:00:00 2001 From: Chris Toph Date: Tue, 4 Mar 2025 14:22:02 -0500 Subject: [PATCH] Enhance Nix configuration: update backup file extension, add SSHFS and bind mounts for user directories, and introduce rebuild script for improved management --- hosts/common/core/default.nix | 2 +- hosts/nixos/vm/hardware.nix | 34 +++++++++++++--- scripts/rebuild.fish | 73 +++++++++++++++++++++++++++++++++++ shell.nix | 28 ++++++++++++++ 4 files changed, 131 insertions(+), 6 deletions(-) create mode 100644 scripts/rebuild.fish create mode 100644 shell.nix diff --git a/hosts/common/core/default.nix b/hosts/common/core/default.nix index d7af836..85c154b 100644 --- a/hosts/common/core/default.nix +++ b/hosts/common/core/default.nix @@ -41,7 +41,7 @@ in # Force home-manager to use global packages home-manager.useGlobalPkgs = true; # If there is a conflict file that is backed up, use this extension - home-manager.backupFileExtension = "bk"; + home-manager.backupFileExtension = "homeManagerBackupFileExtension"; # home-manager.useUserPackages = true; ## Overlays ## diff --git a/hosts/nixos/vm/hardware.nix b/hosts/nixos/vm/hardware.nix index 43b2486..a4033cd 100644 --- a/hosts/nixos/vm/hardware.nix +++ b/hosts/nixos/vm/hardware.nix @@ -8,7 +8,9 @@ modulesPath, ... }: - +let + username = config.hostSpec.username; +in { imports = [ (modulesPath + "/profiles/qemu-guest.nix") @@ -41,11 +43,33 @@ extraModulePackages = [ ]; }; - fileSystems."/" = { - device = "/dev/disk/by-uuid/a0b82536-3087-410a-b283-60ea10811ef5"; - fsType = "ext4"; - }; + fileSystems = { + "/" = { + device = "/dev/disk/by-uuid/a0b82536-3087-410a-b283-60ea10811ef5"; + fsType = "ext4"; + }; + "/pool" = { + device = "${username}@104.40.4.24:/pool"; + fsType = "sshfs"; + options = [ + "defaults" + "reconnect" + "_netdev" + "allow_other" + "identityfile=/home/${username}/.ssh/pve" + ]; + }; + + "/home/${username}/git" = { + fsType = "none"; + device = "/pool/git"; + options = [ + "bind" + "nofail" + ]; + }; + }; swapDevices = [ ]; # Enables DHCP on each ethernet and wireless interface. In case of scripted networking diff --git a/scripts/rebuild.fish b/scripts/rebuild.fish new file mode 100644 index 0000000..7e0393a --- /dev/null +++ b/scripts/rebuild.fish @@ -0,0 +1,73 @@ +#!/usr/bin/env fish + +function red + # Usage: red [] + printf "\033[31m[!] %s \033[0m\n" $argv[1] + if test (count $argv) -ge 2 + # If there's a second argument, we eval it and print in red as well + printf "\033[31m[!] %s \033[0m\n" (eval "$argv[2]") + end +end + +function green + # Usage: green [] + printf "\033[32m[+] %s \033[0m\n" $argv[1] + if test (count $argv) -ge 2 + printf "\033[32m[+] %s \033[0m\n" (eval "$argv[2]") + end +end + +function yellow + # Usage: yellow [] + printf "\033[33m[*] %s \033[0m\n" $argv[1] + if test (count $argv) -ge 2 + printf "\033[33m[*] %s \033[0m\n" (eval "$argv[2]") + end +end + +# Build switch arguments +set switch_args "--show-trace" "--impure" "--flake" + +# Check first argument +if test (count $argv) -gt 0 -a "$argv[1]" = "trace" + set switch_args $switch_args "--show-trace" +else if test (count $argv) -gt 0 + set HOST $argv[1] +else + set HOST (hostname) +end + +# Append flake and host switch +set switch_args $switch_args ".#$HOST" "switch" + +green "====== REBUILD ======" + +# Check if `nh` exists +if type -q nh + find ~ -type f -name "*.homeManagerBackupFileExtension" -delete + set -x REPO_PATH (pwd) + nh os switch . -- --impure --show-trace +else + find ~ -type f -name "*.homeManagerBackupFileExtension" -delete + sudo nixos-rebuild $switch_args +end + +# If successful +if test $status -eq 0 + green "====== POST-REBUILD ======" + green "Rebuilt successfully" + + # Check for a clean git working directory + if git diff --exit-code >/dev/null + and git diff --staged --exit-code >/dev/null + # Check if the current HEAD commit is already tagged as buildable + if git tag --points-at HEAD | grep -q buildable + yellow "Current commit is already tagged as buildable" + else + git tag buildable-(date +%Y%m%d%H%M%S) -m '' + green "Tagged current commit as buildable" + end + else + yellow "WARN: There are pending changes that would affect the build succeeding. Commit them before tagging" + end +end diff --git a/shell.nix b/shell.nix new file mode 100644 index 0000000..162d4df --- /dev/null +++ b/shell.nix @@ -0,0 +1,28 @@ +# Shell for bootstrapping flake-enabled nix and other tooling +{ + pkgs ? + # If pkgs is not defined, instantiate nixpkgs from locked commit + let + lock = (builtins.fromJSON (builtins.readFile ./flake.lock)).nodes.nixpkgs.locked; + nixpkgs = fetchTarball { + url = "https://github.com/nixos/nixpkgs/archive/${lock.rev}.tar.gz"; + sha256 = lock.narHash; + }; + in + import nixpkgs { overlays = [ ]; }, + ... +}: +{ + default = pkgs.mkShell { + nativeBuildInputs = builtins.attrValues { + inherit (pkgs) + + nix + home-manager + nh + git + bats # for bash testing + ; + }; + }; +}