nixos/restic: use optionalString/optionalAttrs where possible

This commit is contained in:
Jörg Thalheim 2020-01-30 17:07:21 +00:00
parent 1c9684abd6
commit 4fa2d4b5c3
No known key found for this signature in database
GPG Key ID: 003F2096411B5F92

View File

@ -163,37 +163,19 @@ in
extraOptions = concatMapStrings (arg: " -o ${arg}") backup.extraOptions; extraOptions = concatMapStrings (arg: " -o ${arg}") backup.extraOptions;
resticCmd = "${pkgs.restic}/bin/restic${extraOptions}"; resticCmd = "${pkgs.restic}/bin/restic${extraOptions}";
filesFromTmpFile = "/run/restic-backups-${name}/includes"; filesFromTmpFile = "/run/restic-backups-${name}/includes";
preStartInit = if backup.initialize
then "${resticCmd} snapshots || ${resticCmd} init"
else "";
dynamicFilesFromScript = pkgs.writeScript "dynamicFilesFromScript" backup.dynamicFilesFrom;
preStartFiles = if backup.dynamicFilesFrom != null
then "${dynamicFilesFromScript} > ${filesFromTmpFile}"
else "";
preStartAttr = if (backup.initialize || backup.dynamicFilesFrom != null)
then {
preStart = ''
${preStartInit}
${preStartFiles}
'';
}
else {};
backupPaths = if (backup.dynamicFilesFrom == null) backupPaths = if (backup.dynamicFilesFrom == null)
then concatStringsSep " " backup.paths then concatStringsSep " " backup.paths
else "--files-from ${filesFromTmpFile}"; else "--files-from ${filesFromTmpFile}";
pruneCmd = if (builtins.length backup.pruneOpts > 0) pruneCmd = optional (builtins.length backup.pruneOpts > 0) [
then [ ( resticCmd + " forget --prune " + ( resticCmd + " forget --prune " + (concatStringsSep " " backup.pruneOpts) )
(concatStringsSep " " backup.pruneOpts) ) ( resticCmd + " check" )
( resticCmd + " check" ) ] ];
else [];
in nameValuePair "restic-backups-${name}" ({ in nameValuePair "restic-backups-${name}" ({
environment = { environment = {
RESTIC_PASSWORD_FILE = backup.passwordFile; RESTIC_PASSWORD_FILE = backup.passwordFile;
RESTIC_REPOSITORY = backup.repository; RESTIC_REPOSITORY = backup.repository;
}; };
path = with pkgs; [ path = [ pkgs.openssh ];
openssh
];
restartIfChanged = false; restartIfChanged = false;
serviceConfig = { serviceConfig = {
Type = "oneshot"; Type = "oneshot";
@ -203,9 +185,16 @@ in
} // optionalAttrs (backup.s3CredentialsFile != null) { } // optionalAttrs (backup.s3CredentialsFile != null) {
EnvironmentFile = backup.s3CredentialsFile; EnvironmentFile = backup.s3CredentialsFile;
}; };
} } // optionalAttrs (backup.initialize || backup.dynamicFilesFrom != null) {
// preStartAttr preStart = ''
// optionalAttrs (backup.dynamicFilesFrom != null) { ${optionalString (backup.initialize) ''
${resticCmd} snapshots || ${resticCmd} init
''}
${optionalString (backup.dynamicFilesFrom != null) ''
${pkgs.writeScript "dynamicFilesFromScript" backup.dynamicFilesFrom} > ${filesFromTmpFile}
''}
'';
} // optionalAttrs (backup.dynamicFilesFrom != null) {
postStart = '' postStart = ''
rm ${filesFromTmpFile} rm ${filesFromTmpFile}
''; '';