Skip to content

Commit 60e1bce

Browse files
jflymergify[bot]
authored andcommitted
Add support for restartUnits and reloadUnits for templates
This fixes #634
1 parent c9f6b15 commit 60e1bce

File tree

3 files changed

+64
-19
lines changed

3 files changed

+64
-19
lines changed

modules/sops/templates/default.nix

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,24 @@ in {
6565
File used as the template. When this value is specified, `sops.templates.<name>.content` is ignored.
6666
'';
6767
};
68+
restartUnits = lib.mkOption {
69+
type = lib.types.listOf lib.types.str;
70+
default = [ ];
71+
example = [ "sshd.service" ];
72+
description = ''
73+
Names of units that should be restarted when the rendered template changes.
74+
This works the same way as <xref linkend="opt-systemd.services._name_.restartTriggers" />.
75+
'';
76+
};
77+
reloadUnits = lib.mkOption {
78+
type = lib.types.listOf lib.types.str;
79+
default = [ ];
80+
example = [ "sshd.service" ];
81+
description = ''
82+
Names of units that should be reloaded when the rendered template changes.
83+
This works the same way as <xref linkend="opt-systemd.services._name_.reloadTriggers" />.
84+
'';
85+
};
6886
};
6987
}));
7088
default = { };

pkgs/sops-install-secrets/main.go

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,22 @@ type loggingConfig struct {
5151
}
5252

5353
type template struct {
54-
Name string `json:"name"`
55-
Content string `json:"content"`
56-
Path string `json:"path"`
57-
Mode string `json:"mode"`
58-
Owner *string `json:"owner,omitempty"`
59-
UID int `json:"uid"`
60-
Group *string `json:"group,omitempty"`
61-
GID int `json:"gid"`
62-
File string `json:"file"`
63-
value []byte
64-
mode os.FileMode
65-
content string
66-
owner int
67-
group int
54+
Name string `json:"name"`
55+
Content string `json:"content"`
56+
Path string `json:"path"`
57+
Mode string `json:"mode"`
58+
Owner *string `json:"owner,omitempty"`
59+
UID int `json:"uid"`
60+
Group *string `json:"group,omitempty"`
61+
GID int `json:"gid"`
62+
File string `json:"file"`
63+
RestartUnits []string `json:"restartUnits"`
64+
ReloadUnits []string `json:"reloadUnits"`
65+
value []byte
66+
mode os.FileMode
67+
content string
68+
owner int
69+
group int
6870
}
6971

7072
type manifest struct {
@@ -951,6 +953,8 @@ func handleModifications(isDry bool, logcfg loggingConfig, symlinkPath string, s
951953
if err != nil {
952954
if os.IsNotExist(err) {
953955
// File did not exist before
956+
restart = append(restart, template.RestartUnits...)
957+
reload = append(reload, template.ReloadUnits...)
954958
newTemplates[template.Name] = true
955959
continue
956960
}
@@ -964,6 +968,8 @@ func handleModifications(isDry bool, logcfg loggingConfig, symlinkPath string, s
964968
}
965969

966970
if !bytes.Equal(oldData, newData) {
971+
restart = append(restart, template.RestartUnits...)
972+
reload = append(reload, template.ReloadUnits...)
967973
modifiedTemplates[template.Name] = true
968974
}
969975
}
@@ -1156,7 +1162,8 @@ func writeTemplates(targetDir string, templates map[string]*template, keysGID in
11561162
if !userMode {
11571163
if err := os.Chown(fp, template.owner, template.group); err != nil {
11581164
return fmt.Errorf("cannot change owner/group of '%s' to %d/%d: %w", fp, template.owner, template.group, err)
1159-
} }
1165+
}
1166+
}
11601167
}
11611168
return nil
11621169
}

pkgs/sops-install-secrets/nixos-test.nix

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -344,10 +344,14 @@ in {
344344
reloadUnits = [ "reload-trigger.service" ];
345345
};
346346

347-
templates.test_template.content = ''
348-
this is a template with
349-
a secret: ${config.sops.placeholder.test_key}
350-
'';
347+
templates.test_template = {
348+
content = ''
349+
this is a template with
350+
a secret: ${config.sops.placeholder.test_key}
351+
'';
352+
restartUnits = [ "restart-unit.service" "reload-unit.service" ];
353+
reloadUnits = [ "reload-trigger.service" ];
354+
};
351355
};
352356
system.switch.enable = true;
353357

@@ -421,6 +425,22 @@ in {
421425
machine.succeed("test -f /restarted")
422426
machine.succeed("test -f /reloaded")
423427
428+
# Cleanup the marker files.
429+
machine.succeed("rm /restarted /reloaded")
430+
431+
# Ensure the template is changed
432+
machine.succeed(": > /run/secrets/rendered/test_template")
433+
434+
# The template is changed, now something should happen
435+
machine.succeed("/run/current-system/bin/switch-to-configuration test")
436+
437+
# Ensure something happened
438+
machine.succeed("test -f /restarted")
439+
machine.succeed("test -f /reloaded")
440+
441+
# Cleanup the marker files.
442+
machine.succeed("rm /restarted /reloaded")
443+
424444
with subtest("change detection"):
425445
machine.succeed("rm /run/secrets/test_key")
426446
machine.succeed("rm /run/secrets/rendered/test_template")

0 commit comments

Comments
 (0)