Skip to content

Commit

Permalink
nushell: add settings option
Browse files Browse the repository at this point in the history
  • Loading branch information
JoaquinTrinanes committed Dec 9, 2024
1 parent 6c8db73 commit bd98b3f
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 16 deletions.
5 changes: 3 additions & 2 deletions modules/lib/nushell.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{ lib }: rec {
mkNushellInline = expr: lib.setType "nushell-inline" { inherit expr; };

toNushell = { indent ? "", multiline ? true, asBindings ? false }@args:
isNushellInline = lib.isType "nushell-inline";

toNushell = { indent ? "", multiline ? true, asBindings ? false, }@args:
v:
let
innerIndent = "${indent} ";
Expand All @@ -18,7 +20,6 @@
asBindings = false;
};
concatItems = lib.concatStringsSep introSpace;
isNushellInline = lib.isType "nushell-inline";

generatedBindings = assert lib.assertMsg (badVarNames == [ ])
"Bad Nushell variable names: ${
Expand Down
59 changes: 47 additions & 12 deletions modules/programs/nushell.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{ config, lib, pkgs, ... }:
let
inherit (lib) types;
inherit (lib.hm.nushell) toNushell;
inherit (lib.hm.nushell) isNushellInline toNushell;
cfg = config.programs.nushell;

configDir = if pkgs.stdenv.isDarwin && !config.xdg.enable then
Expand Down Expand Up @@ -40,16 +40,6 @@ in {
meta.maintainers =
[ lib.maintainers.Philipp-M lib.maintainers.joaquintrinanes ];

imports = [
(lib.mkRemovedOptionModule [ "programs" "nushell" "settings" ] ''
Please use
'programs.nushell.configFile' and 'programs.nushell.envFile'
instead.
'')
];

options.programs.nushell = {
enable = lib.mkEnableOption "nushell";

Expand Down Expand Up @@ -128,6 +118,35 @@ in {
'';
};

settings = lib.mkOption {
type = types.attrsOf lib.hm.types.nushellValue;
default = { };
description = ''
Nushell settings. These will be flattened and assigned one by one to `$env.config` to avoid overwriting the default or existing options.
For example:
```nix
{
show_banner = false;
completions.external = {
enable = true;
max_results = 200;
};
}
```
becomes:
```nushell
$env.config.completions.external.enable = true
$env.config.completions.external.max_results = 200
$env.config.show_banner = false
```
'';
example = {
show_banner = false;
history.format = "sqlite";
};
};

shellAliases = lib.mkOption {
type = types.attrsOf types.str;
default = { };
Expand Down Expand Up @@ -167,13 +186,29 @@ in {
home.file = lib.mkMerge [
(let
writeConfig = cfg.configFile != null || cfg.extraConfig != ""
|| aliasesStr != "";
|| aliasesStr != "" || cfg.settings != { };

aliasesStr = lib.concatLines
(lib.mapAttrsToList (k: v: "alias ${k} = ${v}") cfg.shellAliases);
in lib.mkIf writeConfig {
"${configDir}/config.nu".text = lib.mkMerge [
(lib.mkIf (cfg.configFile != null) cfg.configFile.text)
(let
flattenSettings = settings:
let
unravel = prefixes: value:
if (lib.isAttrs value && !isNushellInline value) then
lib.flatten
(map (key: unravel (prefixes ++ [ key ]) value.${key})
(builtins.attrNames value))
else
lib.nameValuePair (lib.concatStringsSep "." prefixes) value;
in lib.listToAttrs (unravel [ ] settings);

flattenedSettings = flattenSettings cfg.settings;
in lib.mkIf (cfg.settings != { }) (lib.concatLines (lib.mapAttrsToList
(key: value: "$env.config.${key} = ${toNushell { } value}")
flattenedSettings)))
cfg.extraConfig
aliasesStr
];
Expand Down
8 changes: 7 additions & 1 deletion tests/modules/programs/nushell/config-expected.nu
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
let $config = {
let config = {
filesize_metric: false
table_mode: rounded
use_ls_colors: true
}

$env.config.display_errors.exit_code = false
$env.config.hooks.pre_execution = [
({|| "pre_execution hook"})
]
$env.config.show_banner = false


alias ll = ls -a
alias lsname = (ls | get name)
9 changes: 8 additions & 1 deletion tests/modules/programs/nushell/example-settings.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
enable = true;

configFile.text = ''
let $config = {
let config = {
filesize_metric: false
table_mode: rounded
use_ls_colors: true
Expand All @@ -28,6 +28,13 @@
"ll" = "ls -a";
};

settings = {
show_banner = false;
display_errors.exit_code = false;
hooks.pre_execution =
[ (lib.hm.nushell.mkNushellInline ''{|| "pre_execution hook"}'') ];
};

environmentVariables = {
FOO = "BAR";
LIST_VALUE = [ "foo" "bar" ];
Expand Down

0 comments on commit bd98b3f

Please sign in to comment.