Signed-off-by: Nikolaos Karaolidis <nick@karaolidis.com>
This commit is contained in:
2024-12-18 23:29:49 +00:00
parent e23e71560f
commit bcad2979bf
10 changed files with 146 additions and 117 deletions

View File

@@ -19,6 +19,7 @@
after = [ "cryptsetup.target" ]; after = [ "cryptsetup.target" ];
unitConfig.DefaultDependencies = false; unitConfig.DefaultDependencies = false;
serviceConfig.Type = "oneshot"; serviceConfig.Type = "oneshot";
environment.DEVICE = config.environment.impermanence.device;
script = builtins.readFile ./scripts/wipe.sh; script = builtins.readFile ./scripts/wipe.sh;
}; };
}; };

View File

@@ -43,125 +43,136 @@ let
) [ ] parents; ) [ ] parents;
in in
{ {
options.environment.persistence = options.environment =
with lib; with lib;
with types; with types;
let {
isPathLike = strings.hasPrefix "/"; impermanence.device = mkOption {
in type = str;
mkOption { default = config.disko.devices.disk.main.content.partitions.root.content.name;
type = ( description = ''
addCheck (attrsOf ( LUKS BTRFS partition to wipe on boot.
attrsOf ( '';
submodule ( };
{ name, config, ... }:
{
options = {
enable = mkOption {
type = bool;
default = true;
description = "Whether to enable the item.";
};
service = mkOption { persistence =
type = str; let
readOnly = true; isPathLike = strings.hasPrefix "/";
description = '' in
Systemd service that prepares and syncs the item. mkOption {
Can be used as a dependency in other units. type = (
''; addCheck (attrsOf (
}; attrsOf (
submodule (
{ name, config, ... }:
{
options = {
enable = mkOption {
type = bool;
default = true;
description = "Whether to enable the item.";
};
mount = mkOption { service = mkOption {
type = str; type = str;
readOnly = true; readOnly = true;
description = '' description = ''
Systemd mount that binds the item. Systemd service that prepares and syncs the item.
Can be used as a dependency in other units. Can be used as a dependency in other units.
''; '';
}; };
_path = mkOption { mount = mkOption {
type = str; type = str;
internal = true; readOnly = true;
default = name; description = ''
}; Systemd mount that binds the item.
Can be used as a dependency in other units.
'';
};
_sourceRoot = mkOption { _path = mkOption {
type = str; type = str;
internal = true; internal = true;
}; default = name;
};
_source = mkOption { _sourceRoot = mkOption {
type = str; type = str;
internal = true; internal = true;
}; };
_targetRoot = mkOption { _source = mkOption {
type = str; type = str;
internal = true; internal = true;
}; };
_target = mkOption { _targetRoot = mkOption {
type = str; type = str;
internal = true; internal = true;
}; };
};
}
)
)
)) (attrs: lists.all isPathLike (builtins.attrNames attrs))
);
apply =
ps:
builtins.mapAttrs (
persistence: items:
builtins.mapAttrs (
_: config:
let
_path = config._path;
_sourceRoot = persistence; _target = mkOption {
type = str;
_source = mergePaths [ internal = true;
_sourceRoot };
_path };
]; }
)
_targetRoot = )
)) (attrs: lists.all isPathLike (builtins.attrNames attrs))
);
apply =
ps:
builtins.mapAttrs (
persistence: items:
builtins.mapAttrs (
_: config:
let let
parents = lists.reverseList (parentsOf _path); _path = config._path;
in
lists.foldl' (
acc: parent:
if acc == "/" then
lists.findFirst (
otherPersistence: lists.any (other: parent == other) (builtins.attrNames ps.${otherPersistence})
) "/" (builtins.attrNames ps)
else
acc
) "/" parents;
_target = mergePaths [ _sourceRoot = persistence;
_targetRoot
_path _source = mergePaths [
]; _sourceRoot
in _path
config ];
// {
inherit _targetRoot =
_sourceRoot let
_source parents = lists.reverseList (parentsOf _path);
_targetRoot in
_target lists.foldl' (
; acc: parent:
service = "${utils.escapeSystemdPath _target}.service"; if acc == "/" then
mount = "${utils.escapeSystemdPath _target}.mount"; lists.findFirst (
} otherPersistence: lists.any (other: parent == other) (builtins.attrNames ps.${otherPersistence})
) items ) "/" (builtins.attrNames ps)
) ps; else
default = { }; acc
description = "Persistence config."; ) "/" parents;
_target = mergePaths [
_targetRoot
_path
];
in
config
// {
inherit
_sourceRoot
_source
_targetRoot
_target
;
service = "${utils.escapeSystemdPath _target}.service";
mount = "${utils.escapeSystemdPath _target}.mount";
}
) items
) ps;
default = { };
description = "Persistence config.";
};
}; };
config = config =

View File

@@ -6,8 +6,13 @@ delete_subvolume_recursively() {
btrfs subvolume delete "$1" btrfs subvolume delete "$1"
} }
if [[ -z "$DEVICE" ]]; then
echo "Error: DEVICE variable is not set."
exit 1
fi
mkdir -p /mnt/btrfs mkdir -p /mnt/btrfs
mount /dev/mapper/luks /mnt/btrfs mount "/dev/mapper/$DEVICE" /mnt/btrfs
if [[ -e /mnt/btrfs/@ ]]; then if [[ -e /mnt/btrfs/@ ]]; then
mkdir -p /mnt/btrfs/@.bak mkdir -p /mnt/btrfs/@.bak

View File

@@ -16,7 +16,10 @@
description = "Start Default Virtual Network for Libvirt"; description = "Start Default Virtual Network for Libvirt";
script = "${config.virtualisation.libvirtd.package}/bin/virsh net-start default"; script = "${config.virtualisation.libvirtd.package}/bin/virsh net-start default";
preStop = "${config.virtualisation.libvirtd.package}/bin/virsh net-destroy default"; preStop = "${config.virtualisation.libvirtd.package}/bin/virsh net-destroy default";
serviceConfig.Type = "oneshot"; serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
};
wantedBy = [ "libvirtd.service" ]; wantedBy = [ "libvirtd.service" ];
after = [ "libvirtd.service" ]; after = [ "libvirtd.service" ];
}; };

View File

@@ -16,8 +16,13 @@ if [[ -e /mnt/btrfs && -n $(mountpoint -q /mnt/btrfs) ]]; then
exit 1 exit 1
fi fi
if [[ -z "$DEVICE" ]]; then
echo "Error: DEVICE variable is not set."
exit 1
fi
mkdir -p /mnt/btrfs mkdir -p /mnt/btrfs
mount /dev/mapper/luks /mnt/btrfs mount "/dev/mapper/$DEVICE" /mnt/btrfs
if [[ -e /mnt/btrfs/@.bak ]]; then if [[ -e /mnt/btrfs/@.bak ]]; then
if [[ -n "$(ls -A /mnt/btrfs/@.bak)" ]]; then if [[ -n "$(ls -A /mnt/btrfs/@.bak)" ]]; then

View File

@@ -1,4 +1,4 @@
{ pkgs, ... }: { config, pkgs, ... }:
{ {
environment.systemPackages = [ environment.systemPackages = [
(pkgs.writeShellApplication { (pkgs.writeShellApplication {
@@ -10,6 +10,7 @@
btrfs-progs btrfs-progs
nix nix
]; ];
runtimeEnv.DEVICE = config.environment.impermanence.device;
text = builtins.readFile ./cleanup.sh; text = builtins.readFile ./cleanup.sh;
}) })
]; ];

View File

@@ -38,7 +38,7 @@
name = "root"; name = "root";
size = "100%"; size = "100%";
content = { content = {
name = "luks"; name = "main";
type = "luks"; type = "luks";
settings = { settings = {
allowDiscards = true; allowDiscards = true;

View File

@@ -1,4 +1,5 @@
{ {
config,
inputs, inputs,
lib, lib,
pkgs, pkgs,
@@ -23,7 +24,6 @@
../common/system/configs/git ../common/system/configs/git
../common/system/configs/gpg-agent ../common/system/configs/gpg-agent
../common/system/configs/impermanence ../common/system/configs/impermanence
../common/system/configs/libvirt
../common/system/configs/lsof ../common/system/configs/lsof
../common/system/configs/ncdu ../common/system/configs/ncdu
../common/system/configs/neovim ../common/system/configs/neovim
@@ -51,7 +51,7 @@
./users/nikara ./users/nikara
]; ];
networking.hostName = "sas"; networking.hostName = "elara";
i18n.defaultLocale = "en_US.UTF-8"; i18n.defaultLocale = "en_US.UTF-8";
sops.defaultSopsFile = ./secrets/secrets.yaml; sops.defaultSopsFile = ./secrets/secrets.yaml;
@@ -110,6 +110,9 @@
]; ];
}; };
environment.impermanence.device =
config.disko.devices.disk.usb.content.partitions.root.content.name;
nixpkgs = { nixpkgs = {
hostPlatform = "x86_64-linux"; hostPlatform = "x86_64-linux";

View File

@@ -4,7 +4,7 @@
}: }:
{ {
disko.devices = { disko.devices = {
disk.main = { disk.usb = {
inherit device; inherit device;
type = "disk"; type = "disk";
content = { content = {
@@ -30,7 +30,7 @@
name = "root"; name = "root";
size = "100%"; size = "100%";
content = { content = {
name = "luks"; name = "usb";
type = "luks"; type = "luks";
settings = { settings = {
allowDiscards = true; allowDiscards = true;

View File

@@ -109,7 +109,7 @@ in
"globalprotect/gateway".sopsFile = ../../../../secrets/sas/secrets.yaml; "globalprotect/gateway".sopsFile = ../../../../secrets/sas/secrets.yaml;
}; };
theme.wallpaper = ../../../../static/wallpapers/clouds.png; theme.wallpaper = ../../../../static/wallpapers/snow.jpg;
programs.obsidian.vaults."Documents/Obsidian/master".enable = true; programs.obsidian.vaults."Documents/Obsidian/master".enable = true;
}; };