1# This module defines global configuration for the xonsh.
2
3{
4 config,
5 lib,
6 pkgs,
7 ...
8}:
9
10let
11
12 cfg = config.programs.xonsh;
13 package = cfg.package.override { inherit (cfg) extraPackages; };
14 bashCompletionPath = "${cfg.bashCompletion.package}/share/bash-completion/bash_completion";
15in
16
17{
18
19 options = {
20
21 programs.xonsh = {
22
23 enable = lib.mkOption {
24 default = false;
25 description = ''
26 Whether to configure xonsh as an interactive shell.
27 '';
28 type = lib.types.bool;
29 };
30
31 package = lib.mkPackageOption pkgs "xonsh" {
32 extraDescription = ''
33 The argument `extraPackages` of this package will be overridden by
34 the option `programs.xonsh.extraPackages`.
35 '';
36 };
37
38 config = lib.mkOption {
39 default = "";
40 description = ''
41 Extra text added to the end of `/etc/xonsh/xonshrc`,
42 the system-wide control file for xonsh.
43 '';
44 type = lib.types.lines;
45 };
46
47 extraPackages = lib.mkOption {
48 default = (ps: [ ]);
49 defaultText = lib.literalExpression "ps: [ ]";
50 example = lib.literalExpression ''
51 ps: with ps; [ numpy xonsh.xontribs.xontrib-vox ]
52 '';
53 type =
54 with lib.types;
55 coercedTo (listOf lib.types.package) (v: (_: v)) (functionTo (listOf lib.types.package));
56 description = ''
57 Xontribs and extra Python packages to be available in xonsh.
58 '';
59 };
60
61 bashCompletion = {
62 enable = lib.mkEnableOption "bash completions for xonsh" // {
63 default = true;
64 };
65 package = lib.mkPackageOption pkgs "bash-completion" { };
66 };
67 };
68
69 };
70
71 config = lib.mkIf cfg.enable {
72
73 environment.etc."xonsh/xonshrc".text = ''
74 # /etc/xonsh/xonshrc: DO NOT EDIT -- this file has been generated automatically.
75
76
77 if not ''${...}.get('__NIXOS_SET_ENVIRONMENT_DONE'):
78 # The NixOS environment and thereby also $PATH
79 # haven't been fully set up at this point. But
80 # `source-bash` below requires `bash` to be on $PATH,
81 # so add an entry with bash's location:
82 $PATH.add('${pkgs.bash}/bin')
83
84 # Stash xonsh's ls alias, so that we don't get a collision
85 # with Bash's ls alias from environment.shellAliases:
86 _ls_alias = aliases.pop('ls', None)
87
88 # Source the NixOS environment config.
89 source-bash "${config.system.build.setEnvironment}"
90
91 # Restore xonsh's ls alias, overriding that from Bash (if any).
92 if _ls_alias is not None:
93 aliases['ls'] = _ls_alias
94 del _ls_alias
95
96 ${lib.optionalString cfg.bashCompletion.enable "$BASH_COMPLETIONS = '${bashCompletionPath}'"}
97
98 ${cfg.config}
99 '';
100
101 environment.systemPackages = [ package ];
102
103 environment.shells = [
104 "/run/current-system/sw/bin/xonsh"
105 "${lib.getExe package}"
106 ];
107 };
108}