2023-01-26 14:29:12 -06:00
|
|
|
{
|
2023-03-13 09:01:16 -05:00
|
|
|
description = "Restic";
|
2023-01-26 14:29:12 -06:00
|
|
|
|
|
|
|
inputs = {
|
|
|
|
nixpkgs = {
|
|
|
|
url = "nixpkgs/nixos-unstable";
|
|
|
|
};
|
|
|
|
flake-utils = {
|
|
|
|
url = "github:numtide/flake-utils";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
outputs = { self, nixpkgs, flake-utils, ... }@inputs:
|
|
|
|
flake-utils.lib.eachDefaultSystem
|
|
|
|
(system:
|
|
|
|
let
|
|
|
|
pkgs = import nixpkgs {
|
|
|
|
inherit system;
|
|
|
|
};
|
|
|
|
in
|
|
|
|
{
|
2023-03-13 09:01:16 -05:00
|
|
|
packages = {
|
|
|
|
restic =
|
|
|
|
let
|
|
|
|
pname = "restic";
|
2023-07-31 22:09:36 -05:00
|
|
|
version = "0.16.0";
|
|
|
|
hash = "sha256-kxxQlU3bKBjCb1aEtcLBmcnPg4KFgFlbFhs9MmbAgk8=";
|
|
|
|
vendorHash = "sha256-m5smEyAt9RxgvUf1pZqIhgja2h8MWfEgjJ4jUgrPMPY=";
|
2023-03-13 09:01:16 -05:00
|
|
|
in
|
|
|
|
pkgs.buildGoModule {
|
2023-04-22 18:38:58 -05:00
|
|
|
inherit pname version vendorHash;
|
2023-03-13 09:01:16 -05:00
|
|
|
|
|
|
|
src = pkgs.fetchFromGitHub {
|
|
|
|
owner = "restic";
|
|
|
|
repo = "restic";
|
|
|
|
rev = "v${version}";
|
2023-04-22 18:38:58 -05:00
|
|
|
hash = hash;
|
2023-03-13 09:01:16 -05:00
|
|
|
};
|
|
|
|
|
2023-07-31 22:09:36 -05:00
|
|
|
env = {
|
|
|
|
RESTIC_TEST_FUSE = "false";
|
|
|
|
};
|
|
|
|
|
2023-03-13 09:01:16 -05:00
|
|
|
patches = [
|
2023-04-04 11:37:34 -05:00
|
|
|
# The TestRestoreWithPermissionFailure test fails in Nix's build sandbox
|
2023-03-13 09:01:16 -05:00
|
|
|
./0001-Skip-testing-restore-with-permission-failure.patch
|
|
|
|
];
|
|
|
|
|
|
|
|
subPackages = [ "cmd/restic" ];
|
|
|
|
|
2023-04-22 18:38:58 -05:00
|
|
|
nativeBuildInputs = [
|
|
|
|
pkgs.installShellFiles
|
|
|
|
pkgs.makeWrapper
|
|
|
|
];
|
2023-03-13 09:01:16 -05:00
|
|
|
|
|
|
|
passthru.tests.restic = pkgs.nixosTests.restic;
|
|
|
|
|
|
|
|
postPatch = ''
|
2023-07-31 22:09:36 -05:00
|
|
|
# rm cmd/restic/integration_fuse_test.go
|
2023-03-13 09:01:16 -05:00
|
|
|
'';
|
|
|
|
|
|
|
|
postInstall = ''
|
|
|
|
wrapProgram $out/bin/restic --prefix PATH : '${pkgs.rclone}/bin'
|
|
|
|
'' + pkgs.lib.optionalString (pkgs.stdenv.hostPlatform == pkgs.stdenv.buildPlatform) ''
|
|
|
|
$out/bin/restic generate \
|
|
|
|
--bash-completion restic.bash \
|
|
|
|
--zsh-completion restic.zsh \
|
|
|
|
--man .
|
|
|
|
installShellCompletion restic.{bash,zsh}
|
|
|
|
installManPage *.1
|
|
|
|
'';
|
|
|
|
|
|
|
|
meta = with pkgs.lib; {
|
|
|
|
homepage = "https://restic.net";
|
|
|
|
description = "A backup program that is fast, efficient and secure";
|
|
|
|
platforms = platforms.linux ++ platforms.darwin;
|
|
|
|
license = licenses.bsd2;
|
|
|
|
maintainers = [ maintainers.mbrgm ];
|
|
|
|
};
|
|
|
|
};
|
2023-04-22 18:38:58 -05:00
|
|
|
|
|
|
|
entrypoint = pkgs.writeTextFile {
|
|
|
|
name = "entrypoint";
|
|
|
|
destination = "/bin/entrypoint";
|
|
|
|
text = pkgs.lib.concatStringsSep "\n" [
|
|
|
|
"#!${pkgs.nushell}/bin/nu"
|
|
|
|
(builtins.readFile ./entrypoint.nu)
|
|
|
|
];
|
|
|
|
executable = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
docker = pkgs.dockerTools.buildLayeredImage {
|
|
|
|
name = "restic";
|
|
|
|
tag = "latest";
|
|
|
|
maxLayers = 2;
|
|
|
|
contents = [
|
|
|
|
];
|
|
|
|
config = {
|
|
|
|
Cmd = [
|
|
|
|
"${self.packages.${system}.entrypoint}/bin/entrypoint"
|
|
|
|
"--rclone"
|
|
|
|
"${pkgs.rclone}/bin/rclone"
|
|
|
|
"--restic"
|
|
|
|
"${self.packages.${system}.restic}/bin/restic"
|
|
|
|
"--cache-dir"
|
|
|
|
"/cache"
|
|
|
|
];
|
|
|
|
Env = [
|
|
|
|
"TZ=UTC"
|
|
|
|
"SSL_CERT_FILE=${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt"
|
|
|
|
];
|
|
|
|
ExposedPorts = { };
|
|
|
|
Volumes = {
|
|
|
|
"/cache" = { };
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
2023-03-13 09:01:16 -05:00
|
|
|
};
|
2023-01-26 14:29:12 -06:00
|
|
|
}
|
|
|
|
) // {
|
2023-03-13 09:01:16 -05:00
|
|
|
nixosModules = {
|
|
|
|
restic = { config, lib, pkgs, ... }:
|
|
|
|
let
|
|
|
|
cfg = config.restic;
|
|
|
|
package = self.packages.${pkgs.system}.restic;
|
|
|
|
in
|
|
|
|
{
|
|
|
|
options = {
|
|
|
|
restic = lib.options.mkOption {
|
|
|
|
type = lib.types.submodule {
|
|
|
|
options = {
|
|
|
|
enable = lib.options.mkEnableOption "Restic";
|
2023-04-04 11:16:35 -05:00
|
|
|
passwordFile = lib.options.mkOption {
|
2023-04-04 16:34:42 -05:00
|
|
|
type = lib.types.path;
|
2023-01-26 14:29:12 -06:00
|
|
|
};
|
2023-03-13 09:01:16 -05:00
|
|
|
storage = lib.options.mkOption {
|
|
|
|
type = lib.types.enum [
|
|
|
|
"azure"
|
|
|
|
"b2"
|
|
|
|
];
|
|
|
|
};
|
|
|
|
healthcheck = lib.options.mkOption {
|
|
|
|
type = lib.types.submodule {
|
|
|
|
options = {
|
|
|
|
enable = lib.options.mkEnableOption "use healthcheck";
|
2023-04-04 16:34:42 -05:00
|
|
|
apiUrl = lib.options.mkOption {
|
2023-03-13 09:01:16 -05:00
|
|
|
type = lib.types.str;
|
|
|
|
};
|
2023-04-04 16:34:42 -05:00
|
|
|
apiKeyFile = lib.options.mkOption {
|
2023-03-13 09:01:16 -05:00
|
|
|
type = lib.types.str;
|
|
|
|
};
|
|
|
|
timeout = lib.options.mkOption {
|
|
|
|
type = lib.types.int;
|
|
|
|
default = 86400;
|
|
|
|
};
|
|
|
|
grace = lib.options.mkOption {
|
|
|
|
type = lib.types.int;
|
|
|
|
default = 14400;
|
|
|
|
};
|
2023-01-26 14:29:12 -06:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
2023-03-13 09:01:16 -05:00
|
|
|
b2 = lib.options.mkOption {
|
|
|
|
type = lib.types.submodule {
|
|
|
|
options = {
|
|
|
|
bucket = lib.options.mkOption {
|
|
|
|
type = lib.types.str;
|
|
|
|
};
|
2023-04-04 16:34:42 -05:00
|
|
|
accountId = lib.options.mkOption {
|
2023-03-13 09:01:16 -05:00
|
|
|
type = lib.types.str;
|
|
|
|
};
|
2023-04-04 16:34:42 -05:00
|
|
|
accountKeyFile = lib.options.mkOption {
|
2023-03-13 09:01:16 -05:00
|
|
|
type = lib.types.str;
|
|
|
|
};
|
2023-01-26 14:29:12 -06:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
2023-03-13 09:01:16 -05:00
|
|
|
azure = lib.options.mkOption {
|
|
|
|
type = lib.types.submodule {
|
2023-01-26 14:29:12 -06:00
|
|
|
options = {
|
2023-04-04 16:34:42 -05:00
|
|
|
accountName = lib.options.mkOption {
|
2023-03-13 09:01:16 -05:00
|
|
|
type = lib.types.str;
|
2023-01-26 14:29:12 -06:00
|
|
|
};
|
2023-04-04 16:34:42 -05:00
|
|
|
accountKeyFile = lib.options.mkOption {
|
|
|
|
type = lib.types.nullOr lib.types.str;
|
|
|
|
default = null;
|
2023-01-26 14:29:12 -06:00
|
|
|
};
|
|
|
|
};
|
2023-03-13 09:01:16 -05:00
|
|
|
};
|
|
|
|
};
|
|
|
|
backups = lib.options.mkOption {
|
|
|
|
type = lib.types.listOf (
|
|
|
|
lib.types.submodule {
|
|
|
|
options = {
|
2023-04-04 16:34:42 -05:00
|
|
|
oneFileSystem = lib.options.mkOption {
|
2023-03-13 09:01:16 -05:00
|
|
|
type = lib.types.bool;
|
|
|
|
default = false;
|
|
|
|
};
|
|
|
|
excludes = lib.options.mkOption {
|
|
|
|
type = lib.types.listOf lib.types.str;
|
|
|
|
default = [ ];
|
|
|
|
};
|
|
|
|
paths = lib.options.mkOption {
|
|
|
|
type = lib.types.listOf lib.types.str;
|
2023-04-08 09:38:55 -05:00
|
|
|
default = [ ];
|
2023-03-13 09:01:16 -05:00
|
|
|
};
|
2023-03-30 15:43:21 -05:00
|
|
|
preCommand = lib.options.mkOption {
|
2023-03-30 15:50:55 -05:00
|
|
|
type = lib.types.lines;
|
|
|
|
default = "";
|
|
|
|
};
|
|
|
|
postCommand = lib.options.mkOption {
|
|
|
|
type = lib.types.lines;
|
|
|
|
default = "";
|
|
|
|
};
|
2023-03-30 15:40:02 -05:00
|
|
|
};
|
2023-03-13 09:01:16 -05:00
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
2023-01-26 14:29:12 -06:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
2023-04-04 16:34:42 -05:00
|
|
|
|
2023-03-13 09:01:16 -05:00
|
|
|
config =
|
|
|
|
let
|
2023-04-04 16:34:42 -05:00
|
|
|
|
2023-03-13 09:01:16 -05:00
|
|
|
bucket = {
|
|
|
|
"b2" = cfg.b2.bucket;
|
|
|
|
"azure" = builtins.replaceStrings [ "." ] [ "-" ] "${config.networking.hostName}.${config.networking.domain}";
|
|
|
|
}.${cfg.storage};
|
2023-04-04 16:34:42 -05:00
|
|
|
|
2023-03-13 09:01:16 -05:00
|
|
|
directory = {
|
|
|
|
"b2" = "${config.networking.hostName}.${config.networking.domain}";
|
|
|
|
"azure" = "/";
|
|
|
|
}.${cfg.storage};
|
2023-04-04 16:34:42 -05:00
|
|
|
|
2023-03-13 09:01:16 -05:00
|
|
|
in
|
|
|
|
lib.mkIf cfg.enable {
|
2023-04-04 11:16:35 -05:00
|
|
|
|
2023-03-13 09:01:16 -05:00
|
|
|
environment.systemPackages = [
|
|
|
|
package
|
|
|
|
];
|
2023-01-26 14:29:12 -06:00
|
|
|
|
2023-03-13 09:01:16 -05:00
|
|
|
systemd.services.restic =
|
|
|
|
let
|
|
|
|
curlOptions = "--fail --silent --show-error --max-time 10 --retry 5";
|
|
|
|
hcPayload =
|
|
|
|
if cfg.healthcheck.enable
|
2023-01-26 14:29:12 -06:00
|
|
|
then
|
2023-04-04 16:34:42 -05:00
|
|
|
(
|
|
|
|
pkgs.writeText "healthcheck-payload"
|
|
|
|
(
|
|
|
|
builtins.toJSON
|
|
|
|
{
|
|
|
|
name = "${config.networking.hostName}.${config.networking.domain}";
|
|
|
|
timeout = cfg.healthcheck.timeout;
|
|
|
|
grace = cfg.healthcheck.grace;
|
|
|
|
unique = [ "name" ];
|
|
|
|
channels = "*";
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
2023-03-13 09:01:16 -05:00
|
|
|
else
|
2023-04-04 16:34:42 -05:00
|
|
|
null;
|
2023-03-13 09:01:16 -05:00
|
|
|
hcSetup =
|
|
|
|
if cfg.healthcheck.enable
|
|
|
|
then ''
|
2023-04-04 16:34:42 -05:00
|
|
|
HC_URL=''$(${pkgs.curl}/bin/curl ${curlOptions} --request POST --header 'Content-Type: application/json' --header "X-Api-Key: ''$(<${cfg.healthcheck.apiKeyFile})" --data @${hcPayload} "${cfg.healthcheck.apiUrl}" | ${pkgs.jq}/bin/jq -r .ping_url)
|
2023-03-13 09:01:16 -05:00
|
|
|
''
|
|
|
|
else
|
|
|
|
"";
|
|
|
|
hcStart =
|
|
|
|
if cfg.healthcheck.enable
|
|
|
|
then ''
|
2023-04-04 16:34:42 -05:00
|
|
|
if [ ! -z "''${HC_URL}" ]
|
2023-03-13 09:01:16 -05:00
|
|
|
then
|
2023-04-04 16:34:42 -05:00
|
|
|
${pkgs.curl}/bin/curl ${curlOptions} --output /dev/null "''${HC_URL}/start" || true
|
2023-03-13 09:01:16 -05:00
|
|
|
fi
|
|
|
|
''
|
|
|
|
else
|
|
|
|
"";
|
|
|
|
hcStop =
|
|
|
|
if cfg.healthcheck.enable
|
|
|
|
then ''
|
2023-04-04 16:34:42 -05:00
|
|
|
if [ ! -z "''${HC_URL}" ]
|
2023-03-13 09:01:16 -05:00
|
|
|
then
|
2023-04-04 16:34:42 -05:00
|
|
|
${pkgs.curl}/bin/curl ${curlOptions} --output /dev/null "''${HC_URL}" || true
|
2023-03-13 09:01:16 -05:00
|
|
|
fi
|
|
|
|
''
|
|
|
|
else
|
|
|
|
"";
|
2023-04-04 16:34:42 -05:00
|
|
|
|
|
|
|
repositoryConfig = {
|
|
|
|
"b2" = "b2:${bucket}:${directory}";
|
|
|
|
"azure" = "azure:${bucket}:${directory}";
|
|
|
|
}.${cfg.storage};
|
|
|
|
|
|
|
|
resticConfig = ''
|
|
|
|
export RESTIC_CACHE_DIR=/var/cache/restic
|
|
|
|
export RESTIC_PASSWORD_FILE=${cfg.passwordFile}
|
|
|
|
export RESTIC_REPOSITORY=${repositoryConfig}
|
|
|
|
''
|
|
|
|
+ {
|
|
|
|
"b2" = ''
|
|
|
|
export B2_ACCOUNT_ID='${cfg.b2.accountId}'
|
|
|
|
export B2_ACCOUNT_KEY="''$(<${cfg.b2.accountKeyFile})"
|
|
|
|
'';
|
|
|
|
"azure" = ''
|
|
|
|
export AZURE_ACCOUNT_NAME='${cfg.azure.accountName}'
|
|
|
|
export AZURE_ACCOUNT_KEY="''$(<${cfg.azure.accountKeyFile})"
|
|
|
|
'';
|
|
|
|
}.${cfg.storage};
|
|
|
|
|
2023-04-08 09:38:55 -05:00
|
|
|
backupCommands = lib.strings.concatStringsSep "\n"
|
|
|
|
(
|
|
|
|
map
|
|
|
|
(
|
|
|
|
backup:
|
|
|
|
let
|
|
|
|
oneFileSystem =
|
|
|
|
if backup.oneFileSystem
|
|
|
|
then
|
|
|
|
[ "--one-file-system" ]
|
|
|
|
else
|
|
|
|
[ ];
|
|
|
|
excludes = map
|
|
|
|
(
|
|
|
|
exclude: ''--exclude="${exclude}"''
|
|
|
|
)
|
|
|
|
backup.excludes;
|
|
|
|
paths = map
|
|
|
|
(
|
|
|
|
path: ''"${path}"''
|
|
|
|
)
|
|
|
|
backup.paths;
|
|
|
|
arguments = lib.strings.concatStringsSep " " (
|
|
|
|
oneFileSystem ++ excludes ++ paths
|
|
|
|
);
|
|
|
|
in
|
|
|
|
''
|
|
|
|
${backup.preCommand}
|
|
|
|
${package}/bin/restic backup ${arguments}
|
|
|
|
${backup.postCommand}
|
|
|
|
''
|
|
|
|
)
|
|
|
|
cfg.backups
|
|
|
|
);
|
2023-03-13 09:01:16 -05:00
|
|
|
initCheck = {
|
|
|
|
"b2" = ''
|
2023-04-04 16:34:42 -05:00
|
|
|
export RCLONE_CONFIG=/dev/null
|
|
|
|
export RCLONE_CONFIG_B2_TYPE=b2
|
|
|
|
export RCLONE_CONFIG_B2_ACCOUNT='${cfg.b2.accountId}'
|
|
|
|
export RCLONE_CONFIG_B2_KEY="''$(<${cfg.b2.accountKeyFile})"
|
|
|
|
|
|
|
|
config=''$(${pkgs.rclone}/bin/rclone lsjson 'b2:${bucket}/${directory}/config' | ${pkgs.jq}/bin/jq -r '. | length | . > 0')
|
2023-01-26 14:29:12 -06:00
|
|
|
|
2023-04-04 16:34:42 -05:00
|
|
|
if [ "''${config}" != 'true' ]
|
2023-03-13 09:01:16 -05:00
|
|
|
then
|
|
|
|
${package}/bin/restic init
|
|
|
|
fi
|
|
|
|
'';
|
2023-01-26 14:29:12 -06:00
|
|
|
|
2023-03-13 09:01:16 -05:00
|
|
|
"azure" = ''
|
2023-04-04 16:34:42 -05:00
|
|
|
export RCLONE_CONFIG=/dev/null
|
|
|
|
export RCLONE_CONFIG_AZURE_TYPE=azureblob
|
|
|
|
export RCLONE_CONFIG_AZURE_ACCOUNT='${cfg.azure.accountName}'
|
|
|
|
export RCLONE_CONFIG_AZURE_KEY="''$(<${cfg.azure.accountKeyFile})"
|
|
|
|
|
|
|
|
container=''$(${pkgs.rclone}/bin/rclone lsjson 'azure:' | ${pkgs.jq}/bin/jq -r 'map(select(.Path=="${bucket}" and .IsBucket)) | length | . > 0')
|
|
|
|
|
|
|
|
if [ "''${container}" != 'true' ]
|
2023-03-13 09:01:16 -05:00
|
|
|
then
|
2023-04-04 16:34:42 -05:00
|
|
|
${pkgs.rclone}/bin/rclone mkdir 'azure:${bucket}'
|
2023-03-13 09:01:16 -05:00
|
|
|
fi
|
2023-04-04 16:34:42 -05:00
|
|
|
|
|
|
|
config=$(${pkgs.rclone}/bin/rclone lsjson 'azure:${bucket}/config' | ${pkgs.jq}/bin/jq -r '. | length | . > 0')
|
|
|
|
|
|
|
|
if [ "''${config}" != 'true' ]
|
2023-03-13 09:01:16 -05:00
|
|
|
then
|
|
|
|
${package}/bin/restic init
|
|
|
|
fi
|
|
|
|
'';
|
|
|
|
}.${cfg.storage};
|
|
|
|
in
|
|
|
|
{
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
CacheDirectory = "restic";
|
|
|
|
};
|
|
|
|
script = ''
|
|
|
|
${hcSetup}
|
|
|
|
${hcStart}
|
2023-01-26 14:29:12 -06:00
|
|
|
|
2023-04-04 16:34:42 -05:00
|
|
|
${resticConfig}
|
|
|
|
|
2023-03-13 09:01:16 -05:00
|
|
|
${initCheck}
|
2023-01-26 14:29:12 -06:00
|
|
|
|
2023-03-13 09:01:16 -05:00
|
|
|
${backupCommands}
|
2023-01-26 14:29:12 -06:00
|
|
|
|
2023-03-13 09:01:16 -05:00
|
|
|
${package}/bin/restic forget --prune --keep-daily 14
|
|
|
|
${package}/bin/restic check
|
|
|
|
${package}/bin/restic cache --cleanup
|
2023-01-26 14:29:12 -06:00
|
|
|
|
2023-03-13 09:01:16 -05:00
|
|
|
${hcStop}
|
|
|
|
'';
|
|
|
|
};
|
2023-01-26 14:29:12 -06:00
|
|
|
|
2023-03-13 09:01:16 -05:00
|
|
|
systemd.timers.restic = {
|
|
|
|
timerConfig = {
|
|
|
|
OnCalendar = "*-*-* 02:00:00";
|
|
|
|
RandomizedDelaySec = "60m";
|
|
|
|
};
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
2023-01-26 14:29:12 -06:00
|
|
|
};
|
|
|
|
};
|
2023-03-13 09:01:16 -05:00
|
|
|
};
|
|
|
|
};
|
2023-01-26 14:29:12 -06:00
|
|
|
};
|
|
|
|
}
|