1
0
Fork 0
mirror of https://github.com/nix-community/home-manager.git synced 2025-03-19 14:53:00 +00:00
home-manager/modules/misc/xdg-user-dirs.nix

147 lines
4.6 KiB
Nix
Raw Normal View History

2019-09-05 11:18:43 +02:00
{ config, lib, pkgs, ... }:
let
inherit (lib) literalExpression mkOption types;
2019-09-05 11:18:43 +02:00
cfg = config.xdg.userDirs;
2020-02-02 00:39:17 +01:00
in {
meta.maintainers = with lib.maintainers; [ euxane ];
2019-09-05 11:18:43 +02:00
imports = [
(lib.mkRenamedOptionModule [ "xdg" "userDirs" "publishShare" ] [
2020-02-02 00:39:17 +01:00
"xdg"
"userDirs"
"publicShare"
])
];
2019-09-05 11:18:43 +02:00
options.xdg.userDirs = {
enable = mkOption {
type = types.bool;
default = false;
description = ''
Whether to manage {file}`$XDG_CONFIG_HOME/user-dirs.dirs`.
2019-09-05 11:18:43 +02:00
The generated file is read-only.
'';
};
# Well-known directory list from
# https://gitlab.freedesktop.org/xdg/xdg-user-dirs/blob/master/man/user-dirs.dirs.xml
desktop = mkOption {
type = with types; nullOr (coercedTo path toString str);
default = "${config.home.homeDirectory}/Desktop";
defaultText =
literalExpression ''"''${config.home.homeDirectory}/Desktop"'';
description = "The Desktop directory.";
2019-09-05 11:18:43 +02:00
};
documents = mkOption {
type = with types; nullOr (coercedTo path toString str);
default = "${config.home.homeDirectory}/Documents";
defaultText =
literalExpression ''"''${config.home.homeDirectory}/Documents"'';
description = "The Documents directory.";
2019-09-05 11:18:43 +02:00
};
download = mkOption {
type = with types; nullOr (coercedTo path toString str);
default = "${config.home.homeDirectory}/Downloads";
defaultText =
literalExpression ''"''${config.home.homeDirectory}/Downloads"'';
description = "The Downloads directory.";
2019-09-05 11:18:43 +02:00
};
music = mkOption {
type = with types; nullOr (coercedTo path toString str);
default = "${config.home.homeDirectory}/Music";
defaultText =
literalExpression ''"''${config.home.homeDirectory}/Music"'';
description = "The Music directory.";
2019-09-05 11:18:43 +02:00
};
pictures = mkOption {
type = with types; nullOr (coercedTo path toString str);
default = "${config.home.homeDirectory}/Pictures";
defaultText =
literalExpression ''"''${config.home.homeDirectory}/Pictures"'';
description = "The Pictures directory.";
2019-09-05 11:18:43 +02:00
};
publicShare = mkOption {
type = with types; nullOr (coercedTo path toString str);
default = "${config.home.homeDirectory}/Public";
defaultText =
literalExpression ''"''${config.home.homeDirectory}/Public"'';
description = "The Public share directory.";
2019-09-05 11:18:43 +02:00
};
templates = mkOption {
type = with types; nullOr (coercedTo path toString str);
default = "${config.home.homeDirectory}/Templates";
defaultText =
literalExpression ''"''${config.home.homeDirectory}/Templates"'';
description = "The Templates directory.";
2019-09-05 11:18:43 +02:00
};
videos = mkOption {
type = with types; nullOr (coercedTo path toString str);
default = "${config.home.homeDirectory}/Videos";
defaultText =
literalExpression ''"''${config.home.homeDirectory}/Videos"'';
description = "The Videos directory.";
2019-09-05 11:18:43 +02:00
};
extraConfig = mkOption {
type = with types; attrsOf (coercedTo path toString str);
2019-09-05 11:18:43 +02:00
default = { };
defaultText = literalExpression "{ }";
example = literalExpression ''
{
XDG_MISC_DIR = "''${config.home.homeDirectory}/Misc";
}
'';
description = "Other user directories.";
2019-09-05 11:18:43 +02:00
};
createDirectories =
lib.mkEnableOption "automatic creation of the XDG user directories";
2019-09-05 11:18:43 +02:00
};
config = let
directories = (lib.filterAttrs (n: v: !isNull v) {
XDG_DESKTOP_DIR = cfg.desktop;
XDG_DOCUMENTS_DIR = cfg.documents;
XDG_DOWNLOAD_DIR = cfg.download;
XDG_MUSIC_DIR = cfg.music;
XDG_PICTURES_DIR = cfg.pictures;
XDG_PUBLICSHARE_DIR = cfg.publicShare;
XDG_TEMPLATES_DIR = cfg.templates;
XDG_VIDEOS_DIR = cfg.videos;
}) // cfg.extraConfig;
in lib.mkIf cfg.enable {
assertions = [
(lib.hm.assertions.assertPlatform "xdg.userDirs" pkgs lib.platforms.linux)
];
xdg.configFile."user-dirs.dirs".text = let
# For some reason, these need to be wrapped with quotes to be valid.
wrapped = lib.mapAttrs (_: value: ''"${value}"'') directories;
in lib.generators.toKeyValue { } wrapped;
xdg.configFile."user-dirs.conf".text = "enabled=False";
home.sessionVariables = directories;
home.activation.createXdgUserDirectories = lib.mkIf cfg.createDirectories
(let
directoriesList = lib.attrValues directories;
mkdir =
(dir: ''[[ -L "${dir}" ]] || run mkdir -p $VERBOSE_ARG "${dir}"'');
in lib.hm.dag.entryAfter [ "linkGeneration" ]
(lib.strings.concatMapStringsSep "\n" mkdir directoriesList));
2019-09-05 11:18:43 +02:00
};
}