|
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 alias |
59 | | - # of the form: |
60 | | - # `alias {pkg}="op plugin run -- {pkg}"` |
61 | | - # where `{pkg}` is the executable name of the package |
62 | | - aliases = listToAttrs (map (package: { |
63 | | - name = package; |
64 | | - value = "op plugin run -- ${package}"; |
65 | | - }) pkg-exe-names); |
66 | | - packages = lib.optional (cfg.package != null) cfg.package ++ cfg.plugins; |
67 | | - in mkIf cfg.enable (mkMerge [ |
68 | | - ({ |
69 | | - programs = { |
70 | | - bash.shellAliases = aliases; |
71 | | - zsh.shellAliases = aliases; |
72 | | - fish.shellAliases = aliases; |
73 | | - }; |
74 | | - } // optionalAttrs is-home-manager { |
75 | | - home = { |
76 | | - inherit packages; |
77 | | - sessionVariables = { OP_PLUGINS_SOURCED = "1"; }; |
78 | | - }; |
79 | | - } // optionalAttrs (!is-home-manager) { |
80 | | - environment = { |
81 | | - systemPackages = packages; |
82 | | - variables = { OP_PLUGINS_SOURCED = "1"; }; |
83 | | - }; |
84 | | - }) |
85 | | - ]); |
| 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 alias |
| 71 | + # of the form: |
| 72 | + # `alias {pkg}="op plugin run -- {pkg}"` |
| 73 | + # where `{pkg}` is the executable name of the package |
| 74 | + aliases = listToAttrs ( |
| 75 | + map (package: { |
| 76 | + name = package; |
| 77 | + value = "op plugin run -- ${package}"; |
| 78 | + }) pkg-exe-names |
| 79 | + ); |
| 80 | + packages = lib.optional (cfg.package != null) cfg.package ++ cfg.plugins; |
| 81 | + in |
| 82 | + mkIf cfg.enable (mkMerge [ |
| 83 | + ( |
| 84 | + { |
| 85 | + programs = { |
| 86 | + bash.shellAliases = aliases; |
| 87 | + zsh.shellAliases = aliases; |
| 88 | + fish.shellAliases = aliases; |
| 89 | + }; |
| 90 | + } |
| 91 | + // optionalAttrs is-home-manager { |
| 92 | + home = { |
| 93 | + inherit packages; |
| 94 | + sessionVariables = { |
| 95 | + OP_PLUGINS_SOURCED = "1"; |
| 96 | + }; |
| 97 | + }; |
| 98 | + } |
| 99 | + // optionalAttrs (!is-home-manager) { |
| 100 | + environment = { |
| 101 | + systemPackages = packages; |
| 102 | + variables = { |
| 103 | + OP_PLUGINS_SOURCED = "1"; |
| 104 | + }; |
| 105 | + }; |
| 106 | + } |
| 107 | + ) |
| 108 | + ]); |
86 | 109 | } |
0 commit comments