modules: fs: add a "text" type to populate static text files when symlinks wont do

This commit is contained in:
Colin 2023-08-31 10:32:11 +00:00
parent 815a8b52b6
commit ded5d94d69
2 changed files with 48 additions and 1 deletions

View File

@ -9,6 +9,10 @@ let
pname = "ensure-dir";
src = ./.;
};
ensure-file = pkgs.static-nix-shell.mkBash {
pname = "ensure-file";
src = ./.;
};
ensure-symlink = pkgs.static-nix-shell.mkBash {
pname = "ensure-symlink";
src = ./.;
@ -33,6 +37,10 @@ let
type = types.nullOr dirEntry;
default = null;
};
file = mkOption {
type = types.nullOr (fileEntryFor name);
default = null;
};
symlink = mkOption {
type = types.nullOr (symlinkEntryFor name);
default = null;
@ -81,6 +89,8 @@ let
default-acl
(lib.mkIf (config.dir != null)
(sane-lib.filterNonNull config.dir.acl))
(lib.mkIf (config.file != null)
(sane-lib.filterNonNull config.file.acl))
(lib.mkIf (config.symlink != null)
(sane-lib.filterNonNull config.symlink.acl))
];
@ -88,6 +98,7 @@ let
# actually generate the item
generated.command = lib.mkMerge [
(lib.mkIf (config.dir != null) [ "${ensure-dir}/bin/ensure-dir" name ])
(lib.mkIf (config.file != null) [ "${ensure-file}/bin/ensure-file" name config.file.copyFrom ])
(lib.mkIf (config.symlink != null) [ "${ensure-symlink}/bin/ensure-symlink" name config.symlink.target ])
];
@ -124,7 +135,27 @@ let
# takes no special options
dirEntry = types.submodule propagatedGenerateMod;
symlinkEntryFor = path: types.submodule ({ config, ...}: {
fileEntryFor = path: types.submodule ({ config, ... }: {
options = {
inherit (propagatedGenerateMod.options) acl;
text = mkOption {
type = types.nullOr types.lines;
default = null;
description = "create a file with this text, overwriting anything that was there before.";
};
copyFrom = mkOption {
type = types.coercedTo types.package toString types.str;
description = "populate the file based on the content at this provided path";
};
};
config = {
copyFrom = lib.mkIf (config.text != null) (
pkgs.writeText (path-lib.leaf path) config.text
);
};
});
symlinkEntryFor = path: types.submodule ({ config, ... }: {
options = {
inherit (propagatedGenerateMod.options) acl;
target = mkOption {

16
modules/fs/ensure-file Executable file
View File

@ -0,0 +1,16 @@
#!/usr/bin/env nix-shell
#!nix-shell -i bash
set -e
cpto="$1"
cpfrom="$2"
# -f flag in case the destination perms were set to 000
# --no-dereference in case the destination already exists as a symlink
# however, "no-dereference" has the edge case of copying `cpfrom` to `cpto`
# when `cpto` already exists as a symlink to `cpfom`:
# "cp: <cpto> and <cpfrom> are the same file"
# use `--remove-destination` for that
cp --no-dereference -f "$cpfrom" "$cpto" \
|| cp --no-dereference --remove-destination "$cpfrom" "$cpto"