|
| 1 | +{ pkgs, lib, config, is-home-manager, ... }: |
| 2 | +with lib; |
| 3 | +let |
| 4 | + cfg = config.programs._1password-shell-plugins; |
| 5 | + |
| 6 | + supported_plugins = splitString "\n" (lib.readFile "${ |
| 7 | + # get the list of supported plugin executable names |
| 8 | + 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: |
| 14 | + # NOTE: SAFETY: This is okay because the `packages` list is also referred |
| 15 | + # to below as `home.packages = packages;` or `environment.systemPackages = packages;` |
| 16 | + # depending on if it's using `home-manager` or not; this means that Nix can still |
| 17 | + # compute the dependency tree, even though we're discarding string context here, |
| 18 | + # since the packages are still referred to below without discarding string context. |
| 19 | + strings.unsafeDiscardStringContext (baseNameOf (getExe package)); |
| 20 | +in { |
| 21 | + options = { |
| 22 | + programs._1password-shell-plugins = { |
| 23 | + enable = mkEnableOption "1Password Shell Plugins"; |
| 24 | + plugins = mkOption { |
| 25 | + type = types.listOf types.package; |
| 26 | + default = [ ]; |
| 27 | + example = literalExpression '' |
| 28 | + with pkgs; [ |
| 29 | + gh |
| 30 | + awscli2 |
| 31 | + cachix |
| 32 | + ] |
| 33 | + ''; |
| 34 | + description = |
| 35 | + "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/"; |
| 36 | + # this is a bit of a hack to do option validation; |
| 37 | + # ensure that the list of packages include only packages |
| 38 | + # for which the executable has a supported 1Password Shell Plugin |
| 39 | + apply = package_list: |
| 40 | + map (package: |
| 41 | + if (elem (getExeName package) supported_plugins) then |
| 42 | + package |
| 43 | + else |
| 44 | + abort "${ |
| 45 | + getExeName package |
| 46 | + } 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/") |
| 47 | + package_list; |
| 48 | + }; |
| 49 | + }; |
| 50 | + }; |
| 51 | + |
| 52 | + config = let |
| 53 | + # executable names as strings, e.g. `pkgs.gh` => `"gh"`, `pkgs.awscli2` => `"aws"` |
| 54 | + pkg-exe-names = map getExeName cfg.plugins; |
| 55 | + # Explanation: |
| 56 | + # Map over `cfg.plugins` (the value of the `plugins` option provided by the user) |
| 57 | + # and for each package specified, get the executable name, then create a shell alias |
| 58 | + # of the form: |
| 59 | + # `alias {pkg}="op plugin run -- {pkg}"` |
| 60 | + # where `{pkg}` is the executable name of the package |
| 61 | + aliases = listToAttrs (map (package: { |
| 62 | + name = package; |
| 63 | + value = "op plugin run -- ${package}"; |
| 64 | + }) pkg-exe-names); |
| 65 | + packages = [ pkgs._1password ] ++ cfg.plugins; |
| 66 | + in mkIf cfg.enable (mkMerge [ |
| 67 | + ({ |
| 68 | + programs = { |
| 69 | + bash.shellAliases = aliases; |
| 70 | + zsh.shellAliases = aliases; |
| 71 | + fish.shellAliases = aliases; |
| 72 | + }; |
| 73 | + } // optionalAttrs is-home-manager { |
| 74 | + home = { |
| 75 | + inherit packages; |
| 76 | + sessionVariables = { OP_PLUGINS_SOURCED = "1"; }; |
| 77 | + }; |
| 78 | + } // optionalAttrs (!is-home-manager) { |
| 79 | + environment = { |
| 80 | + systemPackages = packages; |
| 81 | + variables = { OP_PLUGINS_SOURCED = "1"; }; |
| 82 | + }; |
| 83 | + }) |
| 84 | + ]); |
| 85 | +} |
0 commit comments