fs: allow specifying text for a symlink directly

This commit is contained in:
2023-01-06 15:26:39 +00:00
parent e46ab4ec14
commit b6208e1a19
3 changed files with 22 additions and 7 deletions

View File

@@ -21,7 +21,7 @@ let
default = null;
};
symlink = mkOption {
type = types.nullOr symlinkEntry;
type = types.nullOr (symlinkEntryFor name);
default = null;
};
generated = mkOption {
@@ -111,15 +111,25 @@ let
# takes no special options
dirEntry = types.submodule propagatedGenerateMod;
symlinkEntry = types.submodule {
symlinkEntryFor = path: types.submodule ({ config, ...}: {
options = {
inherit (propagatedGenerateMod.options) acl;
target = mkOption {
type = types.str;
type = types.coercedTo types.package toString types.str;
description = "fs path to link to";
};
text = mkOption {
type = types.nullOr types.str;
default = null;
description = "create a file in the /nix/store with the provided text and use that as the target";
};
};
};
config = {
target = lib.mkIf (config.text != null) (
pkgs.writeText (path-lib.leaf path) config.text
);
};
});
generatedEntry = types.submodule {
options = {

View File

@@ -6,14 +6,14 @@ let
known_hosts_text = builtins.concatStringsSep
"\n"
(builtins.attrValues (import ../pubkeys.nix).hosts);
mkSymlink = target: {
symlink.target = target;
mkSymlink = text: {
symlink.text = text;
wantedBeforeBy = [ "multi-user.target" ];
};
in lib.mkIf config.sane.home-manager.enable {
# ssh key is stored in private storage
sane.persist.home.private = [ ".ssh/id_ed25519" ];
sane.fs."/home/colin/.ssh/id_ed25519.pub" = mkSymlink (builtins.toString (pkgs.writeText "id_ed25519.pub" user_pubkey));
sane.fs."/home/colin/.ssh/id_ed25519.pub" = mkSymlink user_pubkey;
home-manager.users.colin = {
programs.ssh.enable = true;

View File

@@ -1,6 +1,7 @@
{ lib, utils, ... }:
let path = rec {
# split the string path into a list of string components.
# root directory "/" becomes the empty list [].
# implicitly performs normalization so that:
@@ -19,6 +20,10 @@ let path = rec {
hasParent = str: (path.parent str) != (path.norm str);
# return the path from `from` to `to`, but keeping absolute form
# e.g. `pathFrom "/home/colin" "/home/colin/foo/bar"` -> "/foo/bar"
# return the last path component; error on the empty path
leaf = str: lib.last (split str);
from = start: end: let
s = path.norm start;
e = path.norm end;