|
1 | | -{ pkgs, lib, config, is-home-manager, ... }: |
| 1 | +{ |
| 2 | + pkgs, |
| 3 | + lib, |
| 4 | + config, |
| 5 | + is-home-manager, |
| 6 | + ... |
| 7 | +}: |
2 | 8 | with lib; |
3 | 9 | let |
4 | 10 | cfg = config.programs._1password-shell-plugins; |
5 | 11 |
|
6 | | - supported_plugins = splitString "\n" (lib.readFile "${ |
7 | | - # get the list of supported plugin executable names |
| 12 | + supported_plugins = splitString "\n" ( |
| 13 | + lib.readFile "${ |
| 14 | + # get the list of supported plugin executable names |
8 | 15 | pkgs.runCommand "op-plugin-list" { } |
9 | | - # 1Password CLI tries to create the config directory automatically, so set a temp XDG_CONFIG_HOME |
10 | | - # since we don't actually need it for this |
11 | | - "mkdir $out && XDG_CONFIG_HOME=$out ${pkgs._1password}/bin/op plugin list | cut -d ' ' -f1 | tail -n +2 > $out/plugins.txt" |
12 | | - }/plugins.txt"); |
13 | | - getExeName = package: |
| 16 | + # 1Password CLI tries to create the config directory automatically, so set a temp XDG_CONFIG_HOME |
| 17 | + # since we don't actually need it for this |
| 18 | + "mkdir $out && XDG_CONFIG_HOME=$out ${ |
| 19 | + if cfg.package != null then cfg.package else pkgs._1password-cli |
| 20 | + }/bin/op plugin list | cut -d ' ' -f1 | tail -n +2 > $out/plugins.txt" |
| 21 | + }/plugins.txt" |
| 22 | + ); |
| 23 | + getExeName = |
| 24 | + package: |
14 | 25 | # NOTE: SAFETY: This is okay because the `packages` list is also referred |
15 | 26 | # to below as `home.packages = packages;` or `environment.systemPackages = packages;` |
16 | 27 | # depending on if it's using `home-manager` or not; this means that Nix can still |
17 | 28 | # compute the dependency tree, even though we're discarding string context here, |
18 | 29 | # since the packages are still referred to below without discarding string context. |
19 | 30 | strings.unsafeDiscardStringContext (baseNameOf (getExe package)); |
20 | | -in { |
| 31 | +in |
| 32 | +{ |
21 | 33 | options = { |
22 | 34 | programs._1password-shell-plugins = { |
23 | 35 | enable = mkEnableOption "1Password Shell Plugins"; |
|
32 | 44 | cachix |
33 | 45 | ] |
34 | 46 | ''; |
35 | | - description = |
36 | | - "CLI Packages to enable 1Password Shell Plugins for; ensure that a Shell Plugin exists by checking the docs: https://developer.1password.com/docs/cli/shell-plugins/"; |
| 47 | + description = "CLI Packages to enable 1Password Shell Plugins for; ensure that a Shell Plugin exists by checking the docs: https://developer.1password.com/docs/cli/shell-plugins/"; |
37 | 48 | # this is a bit of a hack to do option validation; |
38 | 49 | # ensure that the list of packages include only packages |
39 | 50 | # for which the executable has a supported 1Password Shell Plugin |
40 | | - apply = package_list: |
41 | | - map (package: |
| 51 | + apply = |
| 52 | + package_list: |
| 53 | + map ( |
| 54 | + package: |
42 | 55 | if (elem (getExeName package) supported_plugins) then |
43 | 56 | package |
44 | 57 | else |
45 | | - abort "${ |
46 | | - getExeName package |
47 | | - } is not a valid 1Password Shell Plugin. A list of supported plugins can be found by running `op plugin list` or at: https://developer.1password.com/docs/cli/shell-plugins/") |
48 | | - package_list; |
| 58 | + abort "${getExeName package} is not a valid 1Password Shell Plugin. A list of supported plugins can be found by running `op plugin list` or at: https://developer.1password.com/docs/cli/shell-plugins/" |
| 59 | + ) package_list; |
49 | 60 | }; |
50 | 61 | }; |
51 | 62 | }; |
52 | 63 |
|
53 | | - config = let |
54 | | - # executable names as strings, e.g. `pkgs.gh` => `"gh"`, `pkgs.awscli2` => `"aws"` |
55 | | - pkg-exe-names = map getExeName cfg.plugins; |
56 | | - # Explanation: |
57 | | - # Map over `cfg.plugins` (the value of the `plugins` option provided by the user) |
58 | | - # and for each package specified, get the executable name, then create a shell function |
59 | | - # of the form: |
60 | | - # |
61 | | - # For Bash and Zsh: |
62 | | - # ``` |
63 | | - # {pkg}() { |
64 | | - # op plugin run -- {pkg}; |
65 | | - # } |
66 | | - # ``` |
67 | | - # |
68 | | - # And for Fish: |
69 | | - # ``` |
70 | | - # function {pkg} --wraps {pkg} |
71 | | - # op plugin run -- {pkg} |
72 | | - # end |
73 | | - # ``` |
74 | | - # where `{pkg}` is the executable name of the package |
75 | | - posixFunctions = map (package: '' |
76 | | - ${package}() { |
77 | | - op plugin run -- ${package}; |
78 | | - } |
79 | | - '') pkg-exe-names; |
80 | | - fishFunctions = map (package: '' |
81 | | - function ${package} --wraps "${package}" --description "1Password Shell Plugin for ${package}" |
82 | | - op plugin run -- ${package} |
83 | | - end |
84 | | - '') pkg-exe-names; |
85 | | - packages = lib.optional (cfg.package != null) cfg.package ++ cfg.plugins; |
86 | | - in mkIf cfg.enable (mkMerge [ |
87 | | - ({ |
88 | | - # for Fish its the same option path between NixOS vs. home-manager |
89 | | - fish.interactiveShellInit = strings.concatStringsSep "\n" fishFunctions; |
90 | | - } // optionalAttrs is-home-manager { |
91 | | - programs = { |
92 | | - # for the Bash and Zsh home-manager modules, |
93 | | - # the initExtra option is equivalent to Fish's interactiveShellInit |
94 | | - bash.initExtra = strings.concatStringsSep "\n" posixFunctions; |
95 | | - zsh.initExtra = strings.concatStringsSep "\n" posixFunctions; |
96 | | - }; |
97 | | - home = { |
98 | | - inherit packages; |
99 | | - sessionVariables = { OP_PLUGINS_SOURCED = "1"; }; |
100 | | - }; |
101 | | - } // optionalAttrs (!is-home-manager) { |
102 | | - programs = { |
103 | | - bash.interactiveShellInit = |
104 | | - strings.concatStringsSep "\n" posixFunctions; |
105 | | - zsh.interactiveShellInit = strings.concatStringsSep "\n" posixFunctions; |
106 | | - }; |
107 | | - environment = { |
108 | | - systemPackages = packages; |
109 | | - variables = { OP_PLUGINS_SOURCED = "1"; }; |
110 | | - }; |
111 | | - }) |
112 | | - ]); |
| 64 | + config = |
| 65 | + let |
| 66 | + # executable names as strings, e.g. `pkgs.gh` => `"gh"`, `pkgs.awscli2` => `"aws"` |
| 67 | + pkg-exe-names = map getExeName cfg.plugins; |
| 68 | + # Explanation: |
| 69 | + # Map over `cfg.plugins` (the value of the `plugins` option provided by the user) |
| 70 | + # and for each package specified, get the executable name, then create a shell function |
| 71 | + # of the form: |
| 72 | + # |
| 73 | + # For Bash and Zsh: |
| 74 | + # ``` |
| 75 | + # {pkg}() { |
| 76 | + # op plugin run -- {pkg}; |
| 77 | + # } |
| 78 | + # ``` |
| 79 | + # |
| 80 | + # And for Fish: |
| 81 | + # ``` |
| 82 | + # function {pkg} --wraps {pkg} |
| 83 | + # op plugin run -- {pkg} |
| 84 | + # end |
| 85 | + # ``` |
| 86 | + # where `{pkg}` is the executable name of the package |
| 87 | + posixFunctions = map (package: '' |
| 88 | + ${package}() { |
| 89 | + op plugin run -- ${package}; |
| 90 | + } |
| 91 | + '') pkg-exe-names; |
| 92 | + fishFunctions = map (package: '' |
| 93 | + function ${package} --wraps "${package}" --description "1Password Shell Plugin for ${package}" |
| 94 | + op plugin run -- ${package} |
| 95 | + end |
| 96 | + '') pkg-exe-names; |
| 97 | + packages = lib.optional (cfg.package != null) cfg.package ++ cfg.plugins; |
| 98 | + in |
| 99 | + mkIf cfg.enable (mkMerge [ |
| 100 | + ( |
| 101 | + { |
| 102 | + # for Fish its the same option path between NixOS vs. home-manager |
| 103 | + fish.interactiveShellInit = strings.concatStringsSep "\n" fishFunctions; |
| 104 | + } |
| 105 | + // optionalAttrs is-home-manager { |
| 106 | + programs = { |
| 107 | + # for the Bash and Zsh home-manager modules, |
| 108 | + # the initExtra option is equivalent to Fish's interactiveShellInit |
| 109 | + bash.initExtra = strings.concatStringsSep "\n" posixFunctions; |
| 110 | + zsh.initExtra = strings.concatStringsSep "\n" posixFunctions; |
| 111 | + }; |
| 112 | + home = { |
| 113 | + inherit packages; |
| 114 | + sessionVariables = { |
| 115 | + OP_PLUGINS_SOURCED = "1"; |
| 116 | + }; |
| 117 | + }; |
| 118 | + } |
| 119 | + // optionalAttrs (!is-home-manager) { |
| 120 | + programs = { |
| 121 | + bash.interactiveShellInit = |
| 122 | + strings.concatStringsSep "\n" posixFunctions; |
| 123 | + zsh.interactiveShellInit = strings.concatStringsSep "\n" posixFunctions; |
| 124 | + }; |
| 125 | + environment = { |
| 126 | + systemPackages = packages; |
| 127 | + variables = { |
| 128 | + OP_PLUGINS_SOURCED = "1"; |
| 129 | + }; |
| 130 | + }; |
| 131 | + } |
| 132 | + ) |
| 133 | + ]); |
113 | 134 | } |
0 commit comments