-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefault.nix
More file actions
155 lines (150 loc) · 4.61 KB
/
default.nix
File metadata and controls
155 lines (150 loc) · 4.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
{
lib,
config,
pkgs,
...
}:
{
options.homeModules.applications."1password" =
let
publicKeyType =
with lib.types;
listOf (submodule {
options = {
host = lib.mkOption {
example = "example.com";
type = lib.types.str;
description = "Domain or IP you want to SSH key to work for";
};
proxyJump = lib.mkOption {
default = null;
example = "example@ssh.example.com";
type = lib.types.nullOr lib.types.str;
description = "Optional ProxyJump option";
};
user = lib.mkOption {
default = null;
example = "little-timmy";
type = lib.types.nullOr lib.types.str;
description = "Optional User option";
};
localForwards = lib.mkOption {
type = types.listOf types.attrs;
default = [ ];
};
identitiesOnly = lib.mkOption {
default = true;
example = false;
type = lib.types.bool;
description = "Specifies that ssh should only use the authentication identity explicitly configured in the ~/.ssh/config files or passed on the ssh command-line, even if ssh-agent offers more identities.";
};
file = lib.mkOption {
example = ./your-key.pub;
type = lib.types.path;
description = "Path to your public key";
};
};
});
in
{
enable = lib.mkEnableOption "1Password GUI and client";
ssh = {
configureSSH = lib.mkOption {
default = true;
example = true;
type = lib.types.bool;
description = "Wether to a configure the SSH config for the 1Password agent";
};
additionalPublicKeys = lib.mkOption {
default = [ ];
type = publicKeyType;
description = "List of additional public SSH keys";
};
};
};
config =
let
cfg = config.homeModules.applications."1password";
getFileName = keyPath: builtins.baseNameOf (toString keyPath);
getStablePath = keyPath: "${config.home.homeDirectory}/.ssh/public-keys/${getFileName keyPath}";
generateHomeFileEntries =
keysList:
lib.listToAttrs (
lib.map (key: {
name = ".ssh/public-keys/${getFileName key.file}";
value = {
text = builtins.readFile key.file;
};
}) keysList
);
in
lib.mkIf config.homeModules.applications."1password".enable {
home.packages = with pkgs; [
_1password-cli
_1password-gui
];
programs.ssh =
let
mkMatchBlock =
{
pk,
pj ? null,
u ? null,
lf ? [ ],
io,
}:
lib.hm.dag.entryBefore [ "Host *" ] {
identitiesOnly = io;
identityFile = (toString pk);
proxyJump = pj;
user = u;
localForwards = lf;
};
convertToMatchBlocks =
additionalKeysList:
(builtins.listToAttrs (
lib.map (ak: {
name = ak.host;
value = mkMatchBlock {
pk = (getStablePath ak.file);
pj = ak.proxyJump;
u = ak.user;
io = ak.identitiesOnly;
lf = ak.localForwards;
};
}) additionalKeysList
));
in
lib.mkIf cfg.ssh.configureSSH {
enable = true;
matchBlocks = lib.mkMerge [
{
"github.com" = mkMatchBlock {
pk = "${config.home.homeDirectory}/.ssh/public-keys/github.pub";
io = true;
};
"Host *" = {
host = "*";
extraOptions = {
"IdentityAgent" = "${config.home.homeDirectory}/.1password/agent.sock";
};
};
}
(convertToMatchBlocks cfg.ssh.additionalPublicKeys)
];
};
home.sessionVariables = lib.mkIf cfg.ssh.configureSSH {
SSH_AUTH_SOCK = "~/.1password/agent.sock";
};
home.file = lib.mkIf cfg.ssh.configureSSH (
lib.mkMerge [
{
".ssh/public-keys/github.pub" = {
text = builtins.readFile ./assets/github.pub;
};
}
(generateHomeFileEntries cfg.ssh.additionalPublicKeys)
]
);
};
}