fork/home-manager

git clone https://git.julia.blue/fork/home-manager

Robert HelgessonMerge PR #421575790441

master
4.5 KiB144 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7  cfg = config.xdg.userDirs;
8
9in {
10  meta.maintainers = with maintainers; [ pacien ];
11
12  imports = [
13    (mkRenamedOptionModule [ "xdg" "userDirs" "publishShare" ] [
14      "xdg"
15      "userDirs"
16      "publicShare"
17    ])
18  ];
19
20  options.xdg.userDirs = {
21    enable = mkOption {
22      type = types.bool;
23      default = false;
24      description = ''
25        Whether to manage {file}`$XDG_CONFIG_HOME/user-dirs.dirs`.
26
27        The generated file is read-only.
28      '';
29    };
30
31    # Well-known directory list from
32    # https://gitlab.freedesktop.org/xdg/xdg-user-dirs/blob/master/man/user-dirs.dirs.xml
33
34    desktop = mkOption {
35      type = with types; nullOr (coercedTo path toString str);
36      default = "${config.home.homeDirectory}/Desktop";
37      defaultText =
38        literalExpression ''"''${config.home.homeDirectory}/Desktop"'';
39      description = "The Desktop directory.";
40    };
41
42    documents = mkOption {
43      type = with types; nullOr (coercedTo path toString str);
44      default = "${config.home.homeDirectory}/Documents";
45      defaultText =
46        literalExpression ''"''${config.home.homeDirectory}/Documents"'';
47      description = "The Documents directory.";
48    };
49
50    download = mkOption {
51      type = with types; nullOr (coercedTo path toString str);
52      default = "${config.home.homeDirectory}/Downloads";
53      defaultText =
54        literalExpression ''"''${config.home.homeDirectory}/Downloads"'';
55      description = "The Downloads directory.";
56    };
57
58    music = mkOption {
59      type = with types; nullOr (coercedTo path toString str);
60      default = "${config.home.homeDirectory}/Music";
61      defaultText =
62        literalExpression ''"''${config.home.homeDirectory}/Music"'';
63      description = "The Music directory.";
64    };
65
66    pictures = mkOption {
67      type = with types; nullOr (coercedTo path toString str);
68      default = "${config.home.homeDirectory}/Pictures";
69      defaultText =
70        literalExpression ''"''${config.home.homeDirectory}/Pictures"'';
71      description = "The Pictures directory.";
72    };
73
74    publicShare = mkOption {
75      type = with types; nullOr (coercedTo path toString str);
76      default = "${config.home.homeDirectory}/Public";
77      defaultText =
78        literalExpression ''"''${config.home.homeDirectory}/Public"'';
79      description = "The Public share directory.";
80    };
81
82    templates = mkOption {
83      type = with types; nullOr (coercedTo path toString str);
84      default = "${config.home.homeDirectory}/Templates";
85      defaultText =
86        literalExpression ''"''${config.home.homeDirectory}/Templates"'';
87      description = "The Templates directory.";
88    };
89
90    videos = mkOption {
91      type = with types; nullOr (coercedTo path toString str);
92      default = "${config.home.homeDirectory}/Videos";
93      defaultText =
94        literalExpression ''"''${config.home.homeDirectory}/Videos"'';
95      description = "The Videos directory.";
96    };
97
98    extraConfig = mkOption {
99      type = with types; attrsOf (coercedTo path toString str);
100      default = { };
101      defaultText = literalExpression "{ }";
102      example = literalExpression ''
103        {
104          XDG_MISC_DIR = "''${config.home.homeDirectory}/Misc";
105        }
106      '';
107      description = "Other user directories.";
108    };
109
110    createDirectories =
111      mkEnableOption "automatic creation of the XDG user directories";
112  };
113
114  config = let
115    directories = (filterAttrs (n: v: !isNull v) {
116      XDG_DESKTOP_DIR = cfg.desktop;
117      XDG_DOCUMENTS_DIR = cfg.documents;
118      XDG_DOWNLOAD_DIR = cfg.download;
119      XDG_MUSIC_DIR = cfg.music;
120      XDG_PICTURES_DIR = cfg.pictures;
121      XDG_PUBLICSHARE_DIR = cfg.publicShare;
122      XDG_TEMPLATES_DIR = cfg.templates;
123      XDG_VIDEOS_DIR = cfg.videos;
124    }) // cfg.extraConfig;
125  in mkIf cfg.enable {
126    assertions =
127      [ (hm.assertions.assertPlatform "xdg.userDirs" pkgs platforms.linux) ];
128
129    xdg.configFile."user-dirs.dirs".text = let
130      # For some reason, these need to be wrapped with quotes to be valid.
131      wrapped = mapAttrs (_: value: ''"${value}"'') directories;
132    in generators.toKeyValue { } wrapped;
133
134    xdg.configFile."user-dirs.conf".text = "enabled=False";
135
136    home.sessionVariables = directories;
137
138    home.activation.createXdgUserDirectories = mkIf cfg.createDirectories (let
139      directoriesList = attrValues directories;
140      mkdir = (dir: ''$DRY_RUN_CMD mkdir -p $VERBOSE_ARG "${dir}"'');
141    in lib.hm.dag.entryAfter [ "linkGeneration" ]
142    (strings.concatMapStringsSep "\n" mkdir directoriesList));
143  };
144}