Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

environment.shellAliases: change default behavior #44441

Merged
merged 3 commits into from Oct 13, 2018

Conversation

mnacamura
Copy link
Contributor

@mnacamura mnacamura commented Aug 4, 2018

  • Do not override environment.shellAliases in nixos/modules/programs/shell.nix.
  • programs.{bash,zsh,fish}.shellAliases override environment.shellAliases for each shell.
Motivation for this change

Hopefully improve the situation in #36282.

Things done
  • Tested using sandboxing (nix.useSandbox on NixOS, or option sandbox in nix.conf on non-NixOS)
  • Built on platform(s)
    • NixOS
    • macOS
    • other Linux distributions
  • Tested via one or more NixOS test(s) if existing and applicable for the change (look inside nixos/tests)
  • Tested compilation of all pkgs that depend on this change using nix-shell -p nox --run "nox-review wip"
  • Tested execution of all binary files (usually in ./result/bin/)
  • Determined the impact on package closure size (by running nix path-info -S before and after)
  • Fits CONTRIBUTING.md.

@@ -107,7 +107,11 @@ in
};

environment.shellAliases = mkOption {
default = {};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these defaults intentional?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just moved these aliases that were in nixos/modules/programs/shell.nix to here without any specific reason. Empty default is fine or better I think though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made default environment.shellAliases empty.

Note it was just a remark. I have no opinion really on the matter.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never mind. It just reflected my preference.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverted the default aliases. See #44441 (comment)

@mnacamura
Copy link
Contributor Author

Made default environment.shellAliases empty.

ll = "ls -l";
l = "ls -alh";
};

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not entirely sure about removing those, they have been there for a long time. I don't personally use them, but maybe some people do. Do other distros have any default aliases?

Also, the cost of keeping these around with the changes in this PR isn't very high, so we might as well just leave them in for the sake of not annoying anybody.

Copy link
Contributor Author

@mnacamura mnacamura Oct 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay. Moved them to the default of environment.shellAliases.

@@ -33,7 +33,7 @@ let
'';

bashAliases = concatStringsSep "\n" (
mapAttrsFlatten (k: v: "alias ${k}=${escapeShellArg v}") cfg.shellAliases
mapAttrsFlatten (k: v: "alias ${k}=${escapeShellArg v}") (cfge.shellAliases // cfg.shellAliases)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a better way to implement this is to have

{
  config.programs.bash.shellAliases = mapAttrs (name: mkDefault) cfge.shellAliases;
}

This should then have the // override behavior automatically through the module system. Also this allows the user to query all bash aliases by evaluating the option.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm... By your change, although defined in /etc/nixos/configuration.nix, bash aliases that have the same names as defined in environment.shellAliases seem not appearing in programs.bash.shellAliases.

@infinisil
Copy link
Member

Ah yeah, there's a problem with the original type. This works:

diff --git a/nixos/modules/config/shells-environment.nix b/nixos/modules/config/shells-environment.nix
index 5ecc3cfd207..a279120e125 100644
--- a/nixos/modules/config/shells-environment.nix
+++ b/nixos/modules/config/shells-environment.nix
@@ -108,18 +108,13 @@ in
     };
 
     environment.shellAliases = mkOption {
-      default = {
-        ls = "ls --color=tty";
-        ll = "ls -l";
-        l  = "ls -alh";
-      };
       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. The
         aliases are added to all users' shells.
       '';
-      type = types.attrs; # types.attrsOf types.stringOrPath;
+      type = with types; attrsOf (either str path);
     };
 
     environment.binsh = mkOption {
@@ -161,6 +156,12 @@ in
     # terminal instead of logging out of X11).
     environment.variables = config.environment.sessionVariables;
 
+    environment.shellAliases = mapAttrs (name: mkDefault) {
+      ls = "ls --color=tty";
+      ll = "ls -l";
+      l  = "ls -alh";
+    };
+
     environment.etc."shells".text =
       ''
         ${concatStringsSep "\n" (map utils.toShellPath cfg.shells)}
diff --git a/nixos/modules/programs/bash/bash.nix b/nixos/modules/programs/bash/bash.nix
index 62c63f2aeb3..aa524a333ee 100644
--- a/nixos/modules/programs/bash/bash.nix
+++ b/nixos/modules/programs/bash/bash.nix
@@ -33,7 +33,7 @@ let
   '';
 
   bashAliases = concatStringsSep "\n" (
-    mapAttrsFlatten (k: v: "alias ${k}=${escapeShellArg v}") (cfge.shellAliases // cfg.shellAliases)
+    mapAttrsFlatten (k: v: "alias ${k}=${escapeShellArg v}") cfg.shellAliases
   );
 
 in
@@ -64,7 +64,7 @@ in
           Set of aliases for bash shell, which overrides <option>environment.shellAliases</option>.
           See <option>environment.shellAliases</option> for an option format description.
         '';
-        type = types.attrs; # types.attrsOf types.stringOrPath;
+        type = with types; attrsOf (either str path);
       };
 
       shellInit = mkOption {
@@ -125,6 +125,8 @@ in
 
     programs.bash = {
 
+      shellAliases = mapAttrs (name: mkDefault) cfge.shellAliases;
+
       shellInit = ''
         if [ -z "$__NIXOS_SET_ENVIRONMENT_DONE" ]; then
             . ${config.system.build.setEnvironment}

(the same for fish and zsh)

@mnacamura
Copy link
Contributor Author

The above patch works. Thank you!

Another problem that concerns me is how to remove the default aliases defined in environment.shellAliases. I mean, for example, how to remove l="ls -alh" if it is not necessary.

@infinisil
Copy link
Member

Yeah, usually that's done by allowing null values (prepending the type with nullOr) and handling that as non-presence when generating. Feel free to add support for that as well.

@mnacamura mnacamura force-pushed the shell-aliases branch 3 times, most recently from c75c0be to 8c1092b Compare October 13, 2018 14:53
@mnacamura
Copy link
Contributor Author

All set.

default = {};
default = mapAttrs (name: mkDefault) {
ls = "ls --color=tty";
ll = "ls -l";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this should work like that. It needs to be in the config section. How it's currently will disable all default aliases if a user adds any of their own.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, right. Sorry.

@mnacamura
Copy link
Contributor Author

This time for sure.

@infinisil
Copy link
Member

infinisil commented Oct 13, 2018

This changes the following:

  • programs.{bash,zsh,fish}.shellAliases take defaults from environment.shellAliases, which won't disappear if you set any custom alias.
  • Defaults for environment.shellAliases are l, ll and ls, you can override these by setting enviromnent.shellAliases.{l,ll,ls} for all shells, or programs.{bash,zsh,fish}.shellAliases.{l,ll,ls} for just a specific one.
  • You can unset aliases by setting the value to null

Solves all issues mentioned in #36282

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants