modules: fs: add a "text" type to populate static text files when symlinks wont do
This commit is contained in:
parent
815a8b52b6
commit
ded5d94d69
|
@ -9,6 +9,10 @@ let
|
||||||
pname = "ensure-dir";
|
pname = "ensure-dir";
|
||||||
src = ./.;
|
src = ./.;
|
||||||
};
|
};
|
||||||
|
ensure-file = pkgs.static-nix-shell.mkBash {
|
||||||
|
pname = "ensure-file";
|
||||||
|
src = ./.;
|
||||||
|
};
|
||||||
ensure-symlink = pkgs.static-nix-shell.mkBash {
|
ensure-symlink = pkgs.static-nix-shell.mkBash {
|
||||||
pname = "ensure-symlink";
|
pname = "ensure-symlink";
|
||||||
src = ./.;
|
src = ./.;
|
||||||
|
@ -33,6 +37,10 @@ let
|
||||||
type = types.nullOr dirEntry;
|
type = types.nullOr dirEntry;
|
||||||
default = null;
|
default = null;
|
||||||
};
|
};
|
||||||
|
file = mkOption {
|
||||||
|
type = types.nullOr (fileEntryFor name);
|
||||||
|
default = null;
|
||||||
|
};
|
||||||
symlink = mkOption {
|
symlink = mkOption {
|
||||||
type = types.nullOr (symlinkEntryFor name);
|
type = types.nullOr (symlinkEntryFor name);
|
||||||
default = null;
|
default = null;
|
||||||
|
@ -81,6 +89,8 @@ let
|
||||||
default-acl
|
default-acl
|
||||||
(lib.mkIf (config.dir != null)
|
(lib.mkIf (config.dir != null)
|
||||||
(sane-lib.filterNonNull config.dir.acl))
|
(sane-lib.filterNonNull config.dir.acl))
|
||||||
|
(lib.mkIf (config.file != null)
|
||||||
|
(sane-lib.filterNonNull config.file.acl))
|
||||||
(lib.mkIf (config.symlink != null)
|
(lib.mkIf (config.symlink != null)
|
||||||
(sane-lib.filterNonNull config.symlink.acl))
|
(sane-lib.filterNonNull config.symlink.acl))
|
||||||
];
|
];
|
||||||
|
@ -88,6 +98,7 @@ let
|
||||||
# actually generate the item
|
# actually generate the item
|
||||||
generated.command = lib.mkMerge [
|
generated.command = lib.mkMerge [
|
||||||
(lib.mkIf (config.dir != null) [ "${ensure-dir}/bin/ensure-dir" name ])
|
(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 ])
|
(lib.mkIf (config.symlink != null) [ "${ensure-symlink}/bin/ensure-symlink" name config.symlink.target ])
|
||||||
];
|
];
|
||||||
|
|
||||||
|
@ -124,7 +135,27 @@ let
|
||||||
# takes no special options
|
# takes no special options
|
||||||
dirEntry = types.submodule propagatedGenerateMod;
|
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 = {
|
options = {
|
||||||
inherit (propagatedGenerateMod.options) acl;
|
inherit (propagatedGenerateMod.options) acl;
|
||||||
target = mkOption {
|
target = mkOption {
|
||||||
|
|
16
modules/fs/ensure-file
Executable file
16
modules/fs/ensure-file
Executable 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"
|
Loading…
Reference in New Issue
Block a user