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" ];
unitConfig.DefaultDependencies = false;
serviceConfig.Type = "oneshot";
environment.DEVICE = config.environment.impermanence.device;
script = builtins.readFile ./scripts/wipe.sh;
};
};

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -109,7 +109,7 @@ in
"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;
};