From d3150b5a5acf291b1489506606fc2275140911a2 Mon Sep 17 00:00:00 2001 From: Jacob Bachmann Date: Mon, 22 Jan 2024 00:11:16 +0100 Subject: [PATCH] feat: add nix-update+nvd script --- flake.lock | 12 +++---- modules/aliases.nix | 1 - modules/nix.nix | 12 ++++--- modules/packages.nix | 1 + users/gandalf/modules/default.nix | 1 + users/gandalf/modules/scripts/default.nix | 5 +++ users/gandalf/modules/scripts/nix-update.nix | 35 ++++++++++++++++++++ 7 files changed, 56 insertions(+), 11 deletions(-) create mode 100644 users/gandalf/modules/scripts/default.nix create mode 100644 users/gandalf/modules/scripts/nix-update.nix diff --git a/flake.lock b/flake.lock index cb74b22..de6be50 100644 --- a/flake.lock +++ b/flake.lock @@ -7,11 +7,11 @@ ] }, "locked": { - "lastModified": 1705408632, - "narHash": "sha256-/AhkReVocTli5BLWA5WXxUlGYXn3Agi/uzX76TNrsbo=", + "lastModified": 1705875991, + "narHash": "sha256-ssoo8WYXy4H0qQ81GhzOaMnvDmeXV02D4DdGOYRKCnU=", "owner": "nix-community", "repo": "home-manager", - "rev": "37d6eeceee464adc03585404eebd68765b3c8615", + "rev": "0021558dba313b6f494cd16362dcd4071f407535", "type": "github" }, "original": { @@ -38,11 +38,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1705133751, - "narHash": "sha256-rCIsyE80jgiOU78gCWN3A0wE0tR2GI5nH6MlS+HaaSQ=", + "lastModified": 1705677747, + "narHash": "sha256-eyM3okYtMgYDgmYukoUzrmuoY4xl4FUujnsv/P6I/zI=", "owner": "nixos", "repo": "nixpkgs", - "rev": "9b19f5e77dd906cb52dade0b7bd280339d2a1f3d", + "rev": "bbe7d8f876fbbe7c959c90ba2ae2852220573261", "type": "github" }, "original": { diff --git a/modules/aliases.nix b/modules/aliases.nix index 8d10aa5..d42dffc 100644 --- a/modules/aliases.nix +++ b/modules/aliases.nix @@ -17,7 +17,6 @@ nec = "nvim --cmd \"cd $(readlink -f /etc/nixos)\""; ncc = "sudo nixos-rebuild switch --upgrade-all --flake \"$(readlink -f /etc/nixos)\""; nup = "sudo nix-channel --update"; - nuf = "sudo nix flake update --nix-path /etc/nixos"; ngc = "sudo nix-collect-garbage -d"; dcup = "docker-compose up --detach --remove-orphans"; diff --git a/modules/nix.nix b/modules/nix.nix index 34b92cd..db7b93e 100644 --- a/modules/nix.nix +++ b/modules/nix.nix @@ -1,7 +1,11 @@ -{ ... }: { - nix.settings = { - experimental-features = [ "nix-command" "flakes" ]; - auto-optimise-store = true; +{ pkgs, ... }: { + nix = { + # TODO remove once 24.05 is released + package = pkgs.nixVersions.unstable; + settings = { + experimental-features = [ "nix-command" "flakes" ]; + auto-optimise-store = true; + }; }; nixpkgs.overlays = [ diff --git a/modules/packages.nix b/modules/packages.nix index 7999014..f0bcd3f 100644 --- a/modules/packages.nix +++ b/modules/packages.nix @@ -14,6 +14,7 @@ unzip libsecret xidel # xml parser + nvd # nix diff package versions between two store paths ] ++ lib.optionals (devenv.enable) [ # languages gcc13 diff --git a/users/gandalf/modules/default.nix b/users/gandalf/modules/default.nix index 0d19318..9fb887b 100644 --- a/users/gandalf/modules/default.nix +++ b/users/gandalf/modules/default.nix @@ -8,6 +8,7 @@ ./keyd.nix ./neovim ./packages.nix + ./scripts ./ssh.nix ./tmux.nix ./xdg.nix diff --git a/users/gandalf/modules/scripts/default.nix b/users/gandalf/modules/scripts/default.nix new file mode 100644 index 0000000..3ad0dab --- /dev/null +++ b/users/gandalf/modules/scripts/default.nix @@ -0,0 +1,5 @@ +{ pkgs, nixosConfig, ... }: { + home.packages = [ + (import ./nix-update.nix { inherit pkgs nixosConfig; }) + ]; +} diff --git a/users/gandalf/modules/scripts/nix-update.nix b/users/gandalf/modules/scripts/nix-update.nix new file mode 100644 index 0000000..6e3855f --- /dev/null +++ b/users/gandalf/modules/scripts/nix-update.nix @@ -0,0 +1,35 @@ +{ pkgs, nixosConfig }: + +pkgs.writeShellScriptBin "nuf" '' + reset="\e[0m" + red="\e[31m" + green="\e[32m" + yellow="\e[33m" + white_bold="\e[1;37m" + + function error { + echo "" + echo -e "''${red}>>>Error: $1''${reset}" + exit 1 + } + + FLAKE=$(readlink -f /etc/nixos) + + echo -e "''${green}>>> Updating flake $FLAKE ...''${reset}" + ${nixosConfig.nix.package}/bin/nix flake update --flake "$FLAKE" || error "Updating failed" + + cd $(mktemp -d) + echo "" + echo -e "''${green}>>> Building configuration ...''${reset}" + nixos-rebuild build --flake "$FLAKE" || error "Building failed" + + echo "" + echo -e "''${green}>>> Running diff ...''${reset}"; + ${pkgs.nvd}/bin/nvd diff /run/current-system result || error "Diff failed" + + cd - > /dev/null + + echo "" + echo -e "''${yellow}>>> The new configuration has not been activated.''${reset}" + echo -e "''${yellow}>>> Use ''${white_bold}nixos-rebuild switch''${reset}''${yellow} to activate.''${reset}" +''