1
0
Fork 0
mirror of https://github.com/nix-community/home-manager synced 2024-11-16 08:09:45 +01:00
home-manager/modules/misc/xdg.nix
polykernel 7cb118c9d2
xdg: coerce XDG base directories settings to strings
Currently, when a custom path is set for any of the XDG base
directories (i.e XDG_DATA_HOME, XDG_CONFIG_HOME, ...), the path will
be coerced into a string when consumed by other options such as
xdg.configFile et al. This causes the the given path to be copied to
the nix store which in the case of xdg.configFile et al, translate to
the file being written there as it is a absolute path.

Interestingly, the default base directories all work as intended as
they are encoded as a string.

This commit converts the option to a string regardless of whether it
is a primitive path or a string encoded path. This allows downstream
consumers to use the base directories in arbitrary way without
accidentally copying the content of the directory to the store. It is
implemented in a similar manner as how home.homeDirectory undergoes
string conversion.

The existing file-attr-name test was modified to test also custom xdg
base directories, and the home.file generation test was removed as
there is a dedicated test for this case in the files module. The test
case was renamed to file-gen to better reflect the new scope.
2021-09-21 21:29:44 +02:00

111 lines
3 KiB
Nix

{ options, config, lib, pkgs, ... }:
with lib;
let
cfg = config.xdg;
fileType = (import ../lib/file-type.nix {
inherit (config.home) homeDirectory;
inherit lib pkgs;
}).fileType;
defaultCacheHome = "${config.home.homeDirectory}/.cache";
defaultConfigHome = "${config.home.homeDirectory}/.config";
defaultDataHome = "${config.home.homeDirectory}/.local/share";
getEnvFallback = name: fallback:
let value = builtins.getEnv name;
in if value != "" then value else fallback;
in {
options.xdg = {
enable = mkEnableOption "management of XDG base directories";
cacheHome = mkOption {
type = types.path;
defaultText = "~/.cache";
apply = toString;
description = ''
Absolute path to directory holding application caches.
'';
};
configFile = mkOption {
type = fileType "<varname>xdg.configHome</varname>" cfg.configHome;
default = { };
description = ''
Attribute set of files to link into the user's XDG
configuration home.
'';
};
configHome = mkOption {
type = types.path;
defaultText = "~/.config";
apply = toString;
description = ''
Absolute path to directory holding application configurations.
'';
};
dataFile = mkOption {
type = fileType "<varname>xdg.dataHome</varname>" cfg.dataHome;
default = { };
description = ''
Attribute set of files to link into the user's XDG
data home.
'';
};
dataHome = mkOption {
type = types.path;
defaultText = "~/.local/share";
apply = toString;
description = ''
Absolute path to directory holding application data.
'';
};
};
config = mkMerge [
(mkIf cfg.enable {
xdg.cacheHome = mkDefault defaultCacheHome;
xdg.configHome = mkDefault defaultConfigHome;
xdg.dataHome = mkDefault defaultDataHome;
home.sessionVariables = {
XDG_CACHE_HOME = cfg.cacheHome;
XDG_CONFIG_HOME = cfg.configHome;
XDG_DATA_HOME = cfg.dataHome;
};
})
# Legacy non-deterministic setup.
(mkIf (!cfg.enable && versionOlder config.home.stateVersion "20.09") {
xdg.cacheHome =
mkDefault (getEnvFallback "XDG_CACHE_HOME" defaultCacheHome);
xdg.configHome =
mkDefault (getEnvFallback "XDG_CONFIG_HOME" defaultConfigHome);
xdg.dataHome = mkDefault (getEnvFallback "XDG_DATA_HOME" defaultDataHome);
})
# "Modern" deterministic setup.
(mkIf (!cfg.enable && versionAtLeast config.home.stateVersion "20.09") {
xdg.cacheHome = mkDefault defaultCacheHome;
xdg.configHome = mkDefault defaultConfigHome;
xdg.dataHome = mkDefault defaultDataHome;
})
{
home.file = mkMerge [
(mapAttrs' (name: file: nameValuePair "${cfg.configHome}/${name}" file)
cfg.configFile)
(mapAttrs' (name: file: nameValuePair "${cfg.dataHome}/${name}" file)
cfg.dataFile)
{ "${cfg.cacheHome}/.keep".text = ""; }
];
}
];
}