From d5729dc447e5d8fe8b7850bb5d3e271bd90ee863 Mon Sep 17 00:00:00 2001 From: SebastianStork Date: Mon, 18 Mar 2024 14:09:13 +0100 Subject: [PATCH] Improve nixos-rebuild with nix-helper --- flake.lock | 21 +++++++++++++++++++++ flake.nix | 5 +++++ hosts/dell-laptop/default.nix | 3 +++ modules/home/shell.nix | 26 ++++++++++++++++++++++---- modules/system/default.nix | 1 + modules/system/nix-helper.nix | 14 ++++++++++++++ users/seb/home.nix | 5 ++++- 7 files changed, 70 insertions(+), 5 deletions(-) create mode 100644 modules/system/nix-helper.nix diff --git a/flake.lock b/flake.lock index 4f04c78..3804400 100644 --- a/flake.lock +++ b/flake.lock @@ -20,6 +20,26 @@ "type": "github" } }, + "nh": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1710573382, + "narHash": "sha256-oGPasBk516A6pNE7iBEOmtrNsbqd+frR7O+z9lr1cLw=", + "owner": "viperML", + "repo": "nh", + "rev": "60c019a930e1718f1c5b6638f16dc221bf667d61", + "type": "github" + }, + "original": { + "owner": "viperML", + "repo": "nh", + "type": "github" + } + }, "nix-index-database": { "inputs": { "nixpkgs": [ @@ -75,6 +95,7 @@ "root": { "inputs": { "home-manager": "home-manager", + "nh": "nh", "nix-index-database": "nix-index-database", "nixpkgs": "nixpkgs", "sops-nix": "sops-nix" diff --git a/flake.nix b/flake.nix index 44ca8b7..486782d 100644 --- a/flake.nix +++ b/flake.nix @@ -16,6 +16,11 @@ url = "github:Mic92/sops-nix"; inputs.nixpkgs.follows = "nixpkgs"; }; + + nh = { + url = "github:viperML/nh"; + inputs.nixpkgs.follows = "nixpkgs"; + }; }; outputs = {nixpkgs, ...} @ inputs: let diff --git a/hosts/dell-laptop/default.nix b/hosts/dell-laptop/default.nix index 47a1c16..797fd92 100644 --- a/hosts/dell-laptop/default.nix +++ b/hosts/dell-laptop/default.nix @@ -6,6 +6,8 @@ networking.hostName = "dell-laptop"; + environment.sessionVariables.FLAKE = "/home/seb/Projects/nixos/my-config"; + myConfig = { boot-loader.systemd-boot.enable = true; @@ -24,6 +26,7 @@ sops.enable = true; auto-cpufreq.enable = true; doas.enable = false; + nix-helper.enable = true; }; boot.kernelPackages = pkgs.linuxPackages_latest; diff --git a/modules/home/shell.nix b/modules/home/shell.nix index 5036b37..bc1dd5d 100644 --- a/modules/home/shell.nix +++ b/modules/home/shell.nix @@ -1,6 +1,7 @@ { config, lib, + osConfig, ... }: let cfg = config.myConfig.shell; @@ -9,12 +10,25 @@ in { bash.enable = lib.mkEnableOption ""; zsh.enable = lib.mkEnableOption ""; starship.enable = lib.mkEnableOption ""; - nixAliases.enable = lib.mkEnableOption ""; + nixAliases = { + enable = lib.mkEnableOption ""; + nix-helper.enable = lib.mkEnableOption ""; + }; improvedCommands.enable = lib.mkEnableOption ""; direnv.enable = lib.mkEnableOption ""; }; config = { + assertions = [ + { + assertion = + if cfg.nixAliases.nix-helper.enable + then osConfig.myConfig.nix-helper.enable + else true; + message = "The nix-helper has to be enabled on the OS level."; + } + ]; + programs.bash.enable = cfg.bash.enable; programs.zsh.enable = cfg.zsh.enable; @@ -35,10 +49,14 @@ in { home.shellAliases = let nixAliases = lib.mkIf cfg.nixAliases.enable { - nrs = "sudo nixos-rebuild switch"; - nrb = "sudo nixos-rebuild boot"; + nr = + if cfg.nixAliases.nix-helper.enable + then "nh os" + else "sudo nixos-rebuild --flake $FLAKE"; + nrs = "nr switch"; + nrt = "nr test"; + nrb = "nr boot"; nrrb = "nrb && reboot"; - nrt = "sudo nixos-rebuild test"; nu = "sudo nix flake update"; }; commandAliases = lib.mkIf cfg.improvedCommands.enable { diff --git a/modules/system/default.nix b/modules/system/default.nix index b29c051..3e8e130 100644 --- a/modules/system/default.nix +++ b/modules/system/default.nix @@ -15,5 +15,6 @@ ./dm.nix ./sound.nix ./boot-loader.nix + ./nix-helper.nix ]; } diff --git a/modules/system/nix-helper.nix b/modules/system/nix-helper.nix new file mode 100644 index 0000000..9075de8 --- /dev/null +++ b/modules/system/nix-helper.nix @@ -0,0 +1,14 @@ +{ + inputs, + config, + lib, + ... +}: { + imports = [inputs.nh.nixosModules.default]; + + options.myConfig.nix-helper.enable = lib.mkEnableOption ""; + + config = lib.mkIf config.myConfig.nix-helper.enable { + nh.enable = true; + }; +} diff --git a/users/seb/home.nix b/users/seb/home.nix index 2bd527a..c5988cf 100644 --- a/users/seb/home.nix +++ b/users/seb/home.nix @@ -5,7 +5,10 @@ shell = { bash.enable = true; starship.enable = true; - nixAliases.enable = true; + nixAliases = { + enable = true; + nix-helper.enable = true; + }; improvedCommands.enable = true; direnv.enable = true; };