Skip to content

Commit b976815

Browse files
authored
Merge pull request #541 from 1Password/mrj/push-lzkrynlmlqpr
fix(nix): Resolve warning about 1Password nixpkgs name
2 parents 9449c38 + 4edb39e commit b976815

File tree

1 file changed

+98
-77
lines changed

1 file changed

+98
-77
lines changed

nix/shell-plugins.nix

Lines changed: 98 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,35 @@
1-
{ pkgs, lib, config, is-home-manager, ... }:
1+
{
2+
pkgs,
3+
lib,
4+
config,
5+
is-home-manager,
6+
...
7+
}:
28
with lib;
39
let
410
cfg = config.programs._1password-shell-plugins;
511

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
815
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:
1425
# NOTE: SAFETY: This is okay because the `packages` list is also referred
1526
# to below as `home.packages = packages;` or `environment.systemPackages = packages;`
1627
# depending on if it's using `home-manager` or not; this means that Nix can still
1728
# compute the dependency tree, even though we're discarding string context here,
1829
# since the packages are still referred to below without discarding string context.
1930
strings.unsafeDiscardStringContext (baseNameOf (getExe package));
20-
in {
31+
in
32+
{
2133
options = {
2234
programs._1password-shell-plugins = {
2335
enable = mkEnableOption "1Password Shell Plugins";
@@ -32,82 +44,91 @@ in {
3244
cachix
3345
]
3446
'';
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/";
3748
# this is a bit of a hack to do option validation;
3849
# ensure that the list of packages include only packages
3950
# 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:
4255
if (elem (getExeName package) supported_plugins) then
4356
package
4457
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;
4960
};
5061
};
5162
};
5263

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+
]);
113134
}

0 commit comments

Comments
 (0)