fork/home-manager

git clone https://git.julia.blue/fork/home-manager

jakobkeychain: fix edge-cases in nushell integration0f4e5b49

master
3.2 KiB129 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7  cfg = config.programs.keychain;
8
9  flags = cfg.extraFlags ++ optional (cfg.agents != [ ])
10    "--agents ${concatStringsSep "," cfg.agents}"
11    ++ optional (cfg.inheritType != null) "--inherit ${cfg.inheritType}";
12
13  shellCommand =
14    "${cfg.package}/bin/keychain --eval ${concatStringsSep " " flags} ${
15      concatStringsSep " " cfg.keys
16    }";
17
18in {
19  meta.maintainers = [ ];
20
21  options.programs.keychain = {
22    enable = mkEnableOption "keychain";
23
24    package = mkOption {
25      type = types.package;
26      default = pkgs.keychain;
27      defaultText = literalExpression "pkgs.keychain";
28      description = ''
29        Keychain package to install.
30      '';
31    };
32
33    keys = mkOption {
34      type = types.listOf types.str;
35      default = [ "id_rsa" ];
36      description = ''
37        Keys to add to keychain.
38      '';
39    };
40
41    agents = mkOption {
42      type = types.listOf types.str;
43      default = [ ];
44      description = ''
45        Agents to add.
46      '';
47    };
48
49    inheritType = mkOption {
50      type =
51        types.nullOr (types.enum [ "local" "any" "local-once" "any-once" ]);
52      default = null;
53      description = ''
54        Inherit type to attempt from agent variables from the environment.
55      '';
56    };
57
58    extraFlags = mkOption {
59      type = types.listOf types.str;
60      default = [ "--quiet" ];
61      description = ''
62        Extra flags to pass to keychain.
63      '';
64    };
65
66    enableBashIntegration = mkOption {
67      default = true;
68      type = types.bool;
69      description = ''
70        Whether to enable Bash integration.
71      '';
72    };
73
74    enableFishIntegration = mkOption {
75      default = true;
76      type = types.bool;
77      description = ''
78        Whether to enable Fish integration.
79      '';
80    };
81
82    enableZshIntegration = mkOption {
83      default = true;
84      type = types.bool;
85      description = ''
86        Whether to enable Zsh integration.
87      '';
88    };
89
90    enableNushellIntegration = mkOption {
91      default = true;
92      type = types.bool;
93      description = ''
94        Whether to enable Nushell integration.
95      '';
96    };
97
98    enableXsessionIntegration = mkOption {
99      default = true;
100      type = types.bool;
101      visible = pkgs.stdenv.hostPlatform.isLinux;
102      description = ''
103        Whether to run keychain from your {file}`~/.xsession`.
104      '';
105    };
106  };
107
108  config = mkIf cfg.enable {
109    home.packages = [ cfg.package ];
110    programs.bash.initExtra = mkIf cfg.enableBashIntegration ''
111      eval "$(SHELL=bash ${shellCommand})"
112    '';
113    programs.fish.interactiveShellInit = mkIf cfg.enableFishIntegration ''
114      SHELL=fish eval (${shellCommand})
115    '';
116    programs.zsh.initExtra = mkIf cfg.enableZshIntegration ''
117      eval "$(SHELL=zsh ${shellCommand})"
118    '';
119    programs.nushell.extraConfig = mkIf cfg.enableNushellIntegration ''
120      let keychain_shell_command = (SHELL=bash ${shellCommand}| parse -r '(\w+)=(.*); export \1' | transpose -ird)
121      if not ($keychain_shell_command|is-empty) {
122        $keychain_shell_command | load-env
123      }
124    '';
125    xsession.initExtra = mkIf cfg.enableXsessionIntegration ''
126      eval "$(SHELL=bash ${shellCommand})"
127    '';
128  };
129}