Skip to content

Commit

Permalink
environment.links: add module
Browse files Browse the repository at this point in the history
  • Loading branch information
mkg20001 committed Feb 3, 2025
1 parent 36065e9 commit f3f83a4
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
70 changes: 70 additions & 0 deletions nixos/modules/misc/links.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{ config, lib, pkgs, ... }:

with lib;

let
joinPath = p:
builtins.concatStringsSep "/" ([ "" ] ++ p);
joinLines =
builtins.concatStringsSep "\n";

mkScript = links: joinLines (forEach links (link: let
source = link.source;
target = "/${link.target}";
in
''
mkdir -p "$(dirname ${escapeShellArg target})"
ln -s ${escapeShellArgs [source target]}
''));
in
{
options = {
environment.links = mkOption {
description = "Links to create";
default = {};

type = with types; loaOf (submodule (
{ name, config, ... }:
{ options = {
enable = mkOption {
type = types.bool;
default = true;
description = ''
Whether this link should be generated. This
option allows specific links to be disabled.
'';
};

target = mkOption {
type = types.str;
description = ''
Path of the symlink. Defaults to the
attribute name.
'';
};

source = mkOption {
type = types.path;
description = "Path of the source file.";
};
};

config = {
target = mkDefault name;
};

}));
};
};

config = (let
links' = filter (f: f.enable) (attrValues config.environment.links);
in
mkIf (links' != [])
{
system.activationScripts.links = {
text = mkScript links';
deps = [ ]; # FIXME: add correct deps
};
});
}
2 changes: 2 additions & 0 deletions nixos/modules/module-list.nix
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@
./misc/documentation.nix
./misc/extra-arguments.nix
./misc/ids.nix
./misc/lib.nix
./misc/links.nix
./misc/label.nix
./misc/lib.nix
./misc/locate.nix
Expand Down

0 comments on commit f3f83a4

Please sign in to comment.