nixos-upgrade: Add extraCommands option #1

Merged
batteredbunny merged 3 commits from extraCommands into main 2024-05-25 01:51:43 +03:00
5 changed files with 76 additions and 58 deletions

View file

@ -29,6 +29,8 @@ Adds a few new options to ``system.autoUpgrade``
``failureNotification`` sends a notification to a ntfy topic on an auto upgrade failure ``failureNotification`` sends a notification to a ntfy topic on an auto upgrade failure
``extraCommands`` extra commands to run during auto upgrade
```nix ```nix
system.autoUpgrade = { system.autoUpgrade = {
enable = true; enable = true;

View file

@ -3,18 +3,21 @@
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
}; };
outputs = {nixpkgs, ...}: let outputs = { nixpkgs, ... }:
let
lib = nixpkgs.lib; lib = nixpkgs.lib;
# Stolen from https://github.com/divnix/digga/blob/main/src/importers.nix # Stolen from https://github.com/divnix/digga/blob/main/src/importers.nix
rakeLeaves = dirPath: let rakeLeaves = dirPath:
let
seive = file: type: seive = file: type:
# Only rake `.nix` files or directories # Only rake `.nix` files or directories
(type == "regular" && lib.hasSuffix ".nix" file) || (type == "directory"); (type == "regular" && lib.hasSuffix ".nix" file) || (type == "directory");
collect = file: type: { collect = file: type: {
name = lib.removeSuffix ".nix" file; name = lib.removeSuffix ".nix" file;
value = let value =
let
path = dirPath + "/${file}"; path = dirPath + "/${file}";
in in
if if
@ -27,8 +30,9 @@
files = lib.filterAttrs seive (builtins.readDir dirPath); files = lib.filterAttrs seive (builtins.readDir dirPath);
in in
lib.filterAttrs (n: v: v != {}) (lib.mapAttrs' collect files); lib.filterAttrs (n: v: v != { }) (lib.mapAttrs' collect files);
in { in
{
nixosModules = rakeLeaves ./modules; nixosModules = rakeLeaves ./modules;
}; };
} }

View file

@ -1,40 +1,50 @@
{ { config
config, , lib
lib, , pkgs
pkgs, , ...
... }:
}: let let
cfg = config.system.autoUpgrade; cfg = config.system.autoUpgrade;
updateScript = pkgs.writeShellApplication { updateScript = pkgs.writeShellApplication {
name = "update"; name = "update";
runtimeInputs = with pkgs; [git nix]; runtimeInputs = with pkgs; [ git nix ];
text = '' text = ''
cd ${cfg.flake} cd ${cfg.flake}
git pull git pull
nix flake update --commit-lock-file nix flake update --commit-lock-file
${cfg.extraCommands}
git push git push
''; '';
}; };
in { in
{
options.system.autoUpgrade = { options.system.autoUpgrade = {
# TODO: make sure flake is a local folder # TODO: make sure system.autoUpgrade.flake is a local folder
updateFlake = lib.mkOption { updateFlake = lib.mkOption {
default = false; default = false;
description = "Update lockfile of the flake."; description = "Update lockfile of the flake.";
example = true;
};
extraCommands = lib.mkOption {
type = lib.types.nullOr lib.types.str;
description = "Extra commands to run during upgrade";
}; };
failureNotification = { failureNotification = {
enable = lib.mkOption { enable = lib.mkOption {
default = false; default = false;
description = "Enable ntfy notification on upgrade failure."; description = "Enable ntfy notification on upgrade failure.";
example = true;
}; };
ntfyUrlFile = lib.mkOption { ntfyUrlFile = lib.mkOption {
type = lib.types.nullOr lib.types.path; type = lib.types.nullOr lib.types.path;
description = "Environment file containing NTFY_URL"; description = "Environment file containing NTFY_URL";
example = "/etc/secrets/failureNotification.env";
}; };
}; };
}; };
@ -45,7 +55,7 @@ in {
(lib.getExe updateScript); (lib.getExe updateScript);
nixos-upgrade-failure = lib.mkIf cfg.failureNotification.enable { nixos-upgrade-failure = lib.mkIf cfg.failureNotification.enable {
path = with pkgs; ["/run/wrappers" "/run/current-system/sw" curl]; path = with pkgs; [ "/run/wrappers" "/run/current-system/sw" curl ];
script = '' script = ''
journalctl _SYSTEMD_INVOCATION_ID=`systemctl show --value -p InvocationID nixos-upgrade.service` > /tmp/upgrade-failure.txt journalctl _SYSTEMD_INVOCATION_ID=`systemctl show --value -p InvocationID nixos-upgrade.service` > /tmp/upgrade-failure.txt
curl -T /tmp/upgrade-failure.txt -H "Filename: failure-logs.txt" -H "Title: Nixos auto upgrade failed for $(hostname)" $NTFY_URL curl -T /tmp/upgrade-failure.txt -H "Filename: failure-logs.txt" -H "Title: Nixos auto upgrade failed for $(hostname)" $NTFY_URL
@ -58,6 +68,6 @@ in {
}; };
}; };
nixos-upgrade.onFailure = lib.mkIf cfg.failureNotification.enable ["nixos-upgrade-failure.service"]; nixos-upgrade.onFailure = lib.mkIf cfg.failureNotification.enable [ "nixos-upgrade-failure.service" ];
}; };
} }

View file

@ -1,15 +1,16 @@
{ { config
config, , pkgs
pkgs, , lib
lib, , ...
... }:
}: let let
cfg = config.services.qbittorrent-nox; cfg = config.services.qbittorrent-nox;
in { in
{
options.services.qbittorrent-nox = { options.services.qbittorrent-nox = {
enable = lib.mkEnableOption "qbittorrent, BitTorrent client."; enable = lib.mkEnableOption "qbittorrent, BitTorrent client.";
package = lib.mkPackageOption pkgs "qbittorrent-nox" {}; package = lib.mkPackageOption pkgs "qbittorrent-nox" { };
openFirewall = lib.mkOption { openFirewall = lib.mkOption {
default = false; default = false;
@ -42,13 +43,13 @@ in {
systemd = { systemd = {
services.qbittorrent-nox = { services.qbittorrent-nox = {
description = "qbittorrent BitTorrent client"; description = "qbittorrent BitTorrent client";
wants = ["network-online.target"]; wants = [ "network-online.target" ];
after = [ after = [
"local-fs.target" "local-fs.target"
"network-online.target" "network-online.target"
"nss-lookup.target" "nss-lookup.target"
]; ];
wantedBy = ["multi-user.target"]; wantedBy = [ "multi-user.target" ];
# Needed for running cross-seed's hook # Needed for running cross-seed's hook
# /bin/sh -c "curl -XPOST http://localhost:2468/api/webhook --data-urlencode 'name=%N'" # /bin/sh -c "curl -XPOST http://localhost:2468/api/webhook --data-urlencode 'name=%N'"
@ -87,7 +88,7 @@ in {
MemoryDenyWriteExecute = true; MemoryDenyWriteExecute = true;
SystemCallArchitectures = "native"; SystemCallArchitectures = "native";
CapabilityBoundingSet = ""; CapabilityBoundingSet = "";
SystemCallFilter = ["@system-service"]; SystemCallFilter = [ "@system-service" ];
}; };
}; };
}; };

View file

@ -1,14 +1,15 @@
{ { config
config, , lib
lib, , pkgs
pkgs, , ...
... }:
}: let let
cfg = config.services.unpackerr; cfg = config.services.unpackerr;
in { in
{
options.services.unpackerr = { options.services.unpackerr = {
enable = lib.mkEnableOption "unpackerr"; enable = lib.mkEnableOption "unpackerr";
package = lib.mkPackageOption pkgs "unpackerr" {}; package = lib.mkPackageOption pkgs "unpackerr" { };
user = lib.mkOption { user = lib.mkOption {
type = lib.types.str; type = lib.types.str;
@ -23,7 +24,7 @@ in {
}; };
settings = lib.mkOption { settings = lib.mkOption {
default = {}; default = { };
description = "unpackerr config file"; description = "unpackerr config file";
}; };
@ -43,7 +44,7 @@ in {
User = cfg.user; User = cfg.user;
Group = cfg.group; Group = cfg.group;
ProtectHome = "yes"; ProtectHome = "yes";
DeviceAllow = [""]; DeviceAllow = [ "" ];
LockPersonality = true; LockPersonality = true;
MemoryDenyWriteExecute = true; MemoryDenyWriteExecute = true;
PrivateDevices = true; PrivateDevices = true;
@ -63,7 +64,7 @@ in {
EnvironmentFile = cfg.environmentFile; EnvironmentFile = cfg.environmentFile;
}; };
wantedBy = ["default.target"]; wantedBy = [ "default.target" ];
}; };
}; };
}; };