From 58b8685e47ce54b298c40aff7877ea9b875de0e6 Mon Sep 17 00:00:00 2001 From: Mika Naylor Date: Tue, 27 Dec 2022 17:29:16 +0100 Subject: [PATCH] nushell: add shellAliases option This allows nushell users to define shell aliases that are inserted into nushell's `config.nu`. --- modules/programs/nushell.nix | 21 ++++++++++++++++++- .../programs/nushell/config-expected.nu | 3 +++ .../programs/nushell/example-settings.nix | 5 +++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/modules/programs/nushell.nix b/modules/programs/nushell.nix index da01672b1..9772f711c 100644 --- a/modules/programs/nushell.nix +++ b/modules/programs/nushell.nix @@ -111,17 +111,36 @@ in { Additional configuration to add to the nushell environment variables file. ''; }; + + shellAliases = mkOption { + type = types.attrsOf types.str; + default = { }; + example = { ll = "ls -l"; }; + description = '' + An attribute set that maps aliases (the top level attribute names in + this option) to command strings or directly to build outputs. + ''; + }; }; config = mkIf cfg.enable { home.packages = [ cfg.package ]; + home.file = mkMerge [ - (mkIf (cfg.configFile != null || cfg.extraConfig != "") { + (let + writeConfig = cfg.configFile != null || cfg.extraConfig != "" + || aliasesStr != ""; + + aliasesStr = concatStringsSep "\n" + (mapAttrsToList (k: v: "alias ${k} = ${v}") cfg.shellAliases); + in mkIf writeConfig { "${configDir}/config.nu".text = mkMerge [ (mkIf (cfg.configFile != null) cfg.configFile.text) cfg.extraConfig + aliasesStr ]; }) + (mkIf (cfg.envFile != null || cfg.extraEnv != "") { "${configDir}/env.nu".text = mkMerge [ (mkIf (cfg.envFile != null) cfg.envFile.text) diff --git a/tests/modules/programs/nushell/config-expected.nu b/tests/modules/programs/nushell/config-expected.nu index c70286d70..a306fabd8 100644 --- a/tests/modules/programs/nushell/config-expected.nu +++ b/tests/modules/programs/nushell/config-expected.nu @@ -4,3 +4,6 @@ let $config = { use_ls_colors: true } + +alias ll = ls -a +alias lsname = (ls | get name) \ No newline at end of file diff --git a/tests/modules/programs/nushell/example-settings.nix b/tests/modules/programs/nushell/example-settings.nix index 21d94a70a..28060d7b3 100644 --- a/tests/modules/programs/nushell/example-settings.nix +++ b/tests/modules/programs/nushell/example-settings.nix @@ -15,6 +15,11 @@ envFile.text = '' let-env FOO = 'BAR' ''; + + shellAliases = { + "lsname" = "(ls | get name)"; + "ll" = "ls -a"; + }; }; test.stubs.nushell = { };