From a415bcf94f63eda9dee4f5d6aefc9b8ca5c367ee Mon Sep 17 00:00:00 2001 From: Ethan Carter Edwards Date: Sun, 8 Jun 2025 21:21:33 -0400 Subject: [PATCH 1/2] environment.shellAliases: init Mirrors the NixOS/nix-darwin module that allows one to define shell aliases declaratively. Signed-off-by: Ethan Carter Edwards --- nix/modules/environment.nix | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/nix/modules/environment.nix b/nix/modules/environment.nix index 3cccf66..168d4f1 100644 --- a/nix/modules/environment.nix +++ b/nix/modules/environment.nix @@ -5,6 +5,15 @@ ... }: +let + cfg = config.environment; + + aliases = builtins.concatStringsSep "\n" ( + lib.mapAttrsToList (k: v: "alias -- ${k}=${lib.escapeShellArg v}") ( + lib.filterAttrs (k: v: v != null) cfg.shellAliases + ) + ); +in { options.environment = { systemPackages = lib.mkOption { @@ -16,6 +25,21 @@ type = lib.types.listOf lib.types.str; default = [ ]; }; + + shellAliases = lib.mkOption { + type = with lib.types; attrsOf (nullOr (either str path)); + default = { }; + example = { + nr = "nixpkgs-review pr"; + ".." = "cd .."; + }; + description = '' + An attribute set that maps aliases (the top level attribute names in + this option) to command strings or directly to build outputs. The + aliases are added to all users' shells. + Aliases mapped to `null` are ignored. + ''; + }; }; config = @@ -33,6 +57,10 @@ export PATH=${pathDir}/bin/:''${PATH} ''; + "profile.d/shell-aliases.sh".source = pkgs.writeText "shell-aliases.sh" '' + ${aliases} + ''; + # TODO: figure out how to properly add fish support. We could start by # looking at what NixOS and HM do to set up the fish env. #"fish/conf.d/system-manager-path.fish".source = From d4db6ea72ff9a42821a8368dcddf8eab415a4849 Mon Sep 17 00:00:00 2001 From: Ethan Carter Edwards Date: Sun, 8 Jun 2025 21:48:22 -0400 Subject: [PATCH 2/2] environment.shellAliases: add test Signed-off-by: Ethan Carter Edwards --- examples/example.nix | 4 ++++ test/nix/modules/default.nix | 2 ++ 2 files changed, 6 insertions(+) diff --git a/examples/example.nix b/examples/example.nix index 4a78aa3..5f23699 100644 --- a/examples/example.nix +++ b/examples/example.nix @@ -11,6 +11,10 @@ pkgs.fd ]; + shellAliases = { + nr = "nixpkgs-review pr"; + }; + etc = { foo = { text = '' diff --git a/test/nix/modules/default.nix b/test/nix/modules/default.nix index 0b3c654..cfa9348 100644 --- a/test/nix/modules/default.nix +++ b/test/nix/modules/default.nix @@ -321,6 +321,8 @@ forEachUbuntuImage "example" { vm.succeed("bash --login -c 'realpath $(which rg) | grep -F ${hostPkgs.ripgrep}/bin/rg'") vm.succeed("bash --login -c 'realpath $(which fd) | grep -F ${hostPkgs.fd}/bin/fd'") + vm.succeed("bash --login -c 'type nr'") + ${system-manager.lib.activateProfileSnippet { node = "vm"; profile = newConfig;