Skip to content

Commit

Permalink
Switch to extended Nixpkg's lib
Browse files Browse the repository at this point in the history
This change makes use of the `extend` function inside `lib` to inject
a new `hm` field containing the Home Manager library functions. This
simplifies use of the Home Manager library in the modules and reduces
the risk of accidental infinite recursion.
  • Loading branch information
rycee committed Jan 20, 2020
1 parent c8323a0 commit 8b26499
Show file tree
Hide file tree
Showing 18 changed files with 136 additions and 71 deletions.
19 changes: 12 additions & 7 deletions doc/default.nix
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
{ pkgs }:
{
# Note, this should be "the standard library" + HM extensions.
lib
, pkgs
}:

let

lib = pkgs.lib;

nmdSrc = pkgs.fetchFromGitLab {
name = "nmd";
owner = "rycee";
repo = "nmd";
rev = "9751ca5ef6eb2ef27470010208d4c0a20e89443d";
sha256 = "0rbx10n8kk0bvp1nl5c8q79lz1w0p1b8103asbvwps3gmqd070hi";
rev = "b437898c2b137c39d9c5f9a1cf62ec630f14d9fc";
sha256 = "18j1nh53cfpjpdiwn99x9kqpvr0s7hwngyc0a93xf4sg88ww93lq";
};

nmd = import nmdSrc { inherit pkgs; };
nmd = import nmdSrc { inherit lib pkgs; };

# Make sure the used package is scrubbed to avoid actually
# instantiating derivations.
Expand All @@ -29,7 +31,10 @@ let

hmModulesDocs = nmd.buildModulesDocs {
modules =
import ../modules/modules.nix { inherit lib pkgs; }
import ../modules/modules.nix {
inherit lib pkgs;
check = false;
}
++ [ scrubbedPkgsModule ];
moduleRootPaths = [ ./.. ];
mkModuleUrl = path:
Expand Down
33 changes: 33 additions & 0 deletions doc/release-notes/rl-2003.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,39 @@ home.file = {
Support for the list form will be removed in Home Manager version
20.09.

* The `lib` function attribute given to modules is now enriched with
an attribute `hm` containing extra library functions specific for Home
Manager. More specifically, `lib.hm` is now the same as `config.lib`
and should be the preferred choice since it is more robust.
+
Therefore, if your configuration makes use of, for example,
`config.lib.dag` to create activation script blocks, it is recommended
to change to `lib.hm.dag`.
+
Note, in the unlikely case that you are
+
** using Home Manager's NixOS or nix-darwin module,
** have made your own Home Manager module containing an option named
`config` or `options`, and
** assign to this option in your system configuration inside a plain
attribute set, i.e., without a function argument,

+
then you must update your configuration to perform the option
assignment inside a `config` attribute. For example, instead of
+
[source,nix]
----
home-manager.users.jane = { config = "foo" };
----
+
use
+
[source,nix]
----
home-manager.users.jane = { config.config = "foo" };
----

[[sec-release-20.03-state-version-changes]]
=== State Version Changes

Expand Down
14 changes: 10 additions & 4 deletions modules/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@ let
in
fold f res res.config.warnings;

rawModule = lib.evalModules {
modules =
[ configuration ]
++ (import ./modules.nix { inherit check lib pkgs; });
extendedLib = import ./lib/stdlib-extended.nix pkgs.lib;

hmModules =
import ./modules.nix {
inherit check pkgs;
lib = extendedLib;
};

rawModule = extendedLib.evalModules {
modules = [ configuration ] ++ hmModules;
specialArgs = {
modulesPath = builtins.toString ./.;
};
Expand Down
10 changes: 4 additions & 6 deletions modules/files.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ let

cfg = config.home.file;

dag = config.lib.dag;

homeDirectory = config.home.homeDirectory;

fileType = (import lib/file-type.nix {
Expand Down Expand Up @@ -43,7 +41,7 @@ in
config = {
# This verifies that the links we are about to create will not
# overwrite an existing file.
home.activation.checkLinkTargets = dag.entryBefore ["writeBoundary"] (
home.activation.checkLinkTargets = hm.dag.entryBefore ["writeBoundary"] (
let
check = pkgs.writeText "check" ''
. ${./lib-bash/color-echo.sh}
Expand Down Expand Up @@ -113,7 +111,7 @@ in
# and a failure during the intermediate state FA ∩ FB will not
# result in lost links because this set of links are in both the
# source and target generation.
home.activation.linkGeneration = dag.entryAfter ["writeBoundary"] (
home.activation.linkGeneration = hm.dag.entryAfter ["writeBoundary"] (
let
link = pkgs.writeText "link" ''
newGenFiles="$1"
Expand Down Expand Up @@ -210,7 +208,7 @@ in
''
);

home.activation.checkFilesChanged = dag.entryBefore ["linkGeneration"] (
home.activation.checkFilesChanged = hm.dag.entryBefore ["linkGeneration"] (
''
declare -A changedFiles
'' + concatMapStrings (v: ''
Expand All @@ -220,7 +218,7 @@ in
'') (filter (v: v.onChange != "") (attrValues cfg))
);

home.activation.onFilesChange = dag.entryAfter ["linkGeneration"] (
home.activation.onFilesChange = hm.dag.entryAfter ["linkGeneration"] (
concatMapStrings (v: ''
if [[ ${"$\{changedFiles"}["${v.target}"]} -eq 1 ]]; then
${v.onChange}
Expand Down
13 changes: 5 additions & 8 deletions modules/home-environment.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ let

cfg = config.home;

dag = config.lib.dag;
dagOf = (import ./lib/types.nix { inherit dag lib; }).dagOf;

languageSubModule = types.submodule {
options = {
base = mkOption {
Expand Down Expand Up @@ -235,11 +232,11 @@ in
};

home.activation = mkOption {
type = dagOf types.str;
type = hm.types.dagOf types.str;
default = {};
example = literalExample ''
{
myActivationAction = config.lib.dag.entryAfter ["writeBoundary"] '''
myActivationAction = lib.hm.dag.entryAfter ["writeBoundary"] '''
$DRY_RUN_CMD ln -s $VERBOSE_ARG \
''${builtins.toPath ./link-me-directly} $HOME
''';
Expand Down Expand Up @@ -362,7 +359,7 @@ in

# A dummy entry acting as a boundary between the activation
# script's "check" and the "write" phases.
home.activation.writeBoundary = dag.entryAnywhere "";
home.activation.writeBoundary = hm.dag.entryAnywhere "";

# Install packages to the user environment.
#
Expand All @@ -379,7 +376,7 @@ in
# In case the user has moved from a user-install of Home Manager
# to a submodule managed one we attempt to uninstall the
# `home-manager-path` package if it is installed.
home.activation.installPackages = dag.entryAfter ["writeBoundary"] (
home.activation.installPackages = hm.dag.entryAfter ["writeBoundary"] (
if config.submoduleSupport.externalPackageInstall
then
''
Expand All @@ -399,7 +396,7 @@ in
noteEcho Activating ${res.name}
${res.data}
'';
sortedCommands = dag.topoSort cfg.activation;
sortedCommands = hm.dag.topoSort cfg.activation;
activationCmds =
if sortedCommands ? result then
concatStringsSep "\n" (map mkCmd sortedCommands.result)
Expand Down
3 changes: 2 additions & 1 deletion modules/lib/default.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{ lib }:

{
rec {
dag =
let
d = import ./dag.nix { inherit lib; };
Expand All @@ -17,6 +17,7 @@
};

strings = import ./strings.nix { inherit lib; };
types = import ./types.nix { inherit dag lib; };

shell = import ./shell.nix { inherit lib; };
zsh = import ./zsh.nix { inherit lib; };
Expand Down
11 changes: 11 additions & 0 deletions modules/lib/stdlib-extended.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Just a convenience function that returns the given Nixpkgs standard
# library extended with the HM library.

nixpkgsLib:

let
mkHmLib = import ./.;
in
nixpkgsLib.extend (self: super: {
hm = mkHmLib { lib = super; };
})
1 change: 0 additions & 1 deletion modules/lib/types.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ with lib;

let

hmLib = import ./default.nix { inherit lib; };
typesDag = import ./types-dag.nix { inherit dag lib; };

in
Expand Down
2 changes: 1 addition & 1 deletion modules/manual.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ let

cfg = config.manual;

docs = import ../doc { inherit pkgs; };
docs = import ../doc { inherit lib pkgs; };

in

Expand Down
3 changes: 1 addition & 2 deletions modules/misc/dconf.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ with lib;
let

cfg = config.dconf;
dag = config.lib.dag;

toDconfIni = generators.toINI { mkKeyValue = mkIniKeyValue; };

Expand Down Expand Up @@ -65,7 +64,7 @@ in
};

config = mkIf (cfg.enable && cfg.settings != {}) {
home.activation.dconfSettings = dag.entryAfter ["installPackages"] (
home.activation.dconfSettings = hm.dag.entryAfter ["installPackages"] (
let
iniFile = pkgs.writeText "hm-dconf.ini" (toDconfIni cfg.settings);
in
Expand Down
3 changes: 1 addition & 2 deletions modules/misc/qt.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ with lib;
let

cfg = config.qt;
dag = config.lib.dag;

in

Expand Down Expand Up @@ -69,7 +68,7 @@ in

# Enable GTK+ style for Qt4 in either case.
# It doesn’t support the platform theme packages.
home.activation.useGtkThemeInQt4 = dag.entryAfter ["writeBoundary"] ''
home.activation.useGtkThemeInQt4 = hm.dag.entryAfter ["writeBoundary"] ''
$DRY_RUN_CMD ${pkgs.crudini}/bin/crudini $VERBOSE_ARG \
--set "${config.xdg.configHome}/Trolltech.conf" Qt style GTK+
'';
Expand Down
4 changes: 3 additions & 1 deletion modules/modules.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{ pkgs

# Note, this should be "the standard library" + HM extensions.
, lib

# Whether to enable module type checking.
Expand Down Expand Up @@ -167,7 +169,7 @@ let
config._module.args.baseModules = modules;
config._module.args.pkgs = lib.mkDefault pkgs;
config._module.check = check;
config.lib = import ./lib { inherit lib; };
config.lib = lib.hm;
config.nixpkgs.system = mkDefault pkgs.system;
};

Expand Down
4 changes: 1 addition & 3 deletions modules/programs/info.nix
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ let

cfg = config.programs.info;

dag = config.lib.dag;

# Indexes info files found in this location
homeInfoPath = "${config.home.profileDirectory}/share/info";

Expand Down Expand Up @@ -57,7 +55,7 @@ in
home.sessionVariables.INFOPATH =
"${cfg.homeInfoDirLocation}\${INFOPATH:+:}\${INFOPATH}";

home.activation.createHomeInfoDir = dag.entryAfter ["installPackages"] ''
home.activation.createHomeInfoDir = hm.dag.entryAfter ["installPackages"] ''
oPATH=$PATH
export PATH="${lib.makeBinPath [ pkgs.gzip ]}''${PATH:+:}$PATH"
$DRY_RUN_CMD mkdir -p "${cfg.homeInfoDirLocation}"
Expand Down
4 changes: 1 addition & 3 deletions modules/programs/mbsync.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ with lib;

let

dag = config.lib.dag;

cfg = config.programs.mbsync;

# Accounts for which mbsync is enabled.
Expand Down Expand Up @@ -180,7 +178,7 @@ in

home.activation = mkIf (mbsyncAccounts != []) {
createMaildir =
dag.entryBetween [ "linkGeneration" ] [ "writeBoundary" ] ''
hm.dag.entryBetween [ "linkGeneration" ] [ "writeBoundary" ] ''
$DRY_RUN_CMD mkdir -m700 -p $VERBOSE_ARG ${
concatMapStringsSep " " (a: a.maildir.absPath) mbsyncAccounts
}
Expand Down
4 changes: 1 addition & 3 deletions modules/systemd.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ let

cfg = config.systemd.user;

dag = config.lib.dag;

enabled = cfg.services != {}
|| cfg.sockets != {}
|| cfg.targets != {}
Expand Down Expand Up @@ -230,7 +228,7 @@ in
# running this from the NixOS module then XDG_RUNTIME_DIR is not
# set and systemd commands will fail. We'll therefore have to
# set it ourselves in that case.
home.activation.reloadSystemD = dag.entryAfter ["linkGeneration"] (
home.activation.reloadSystemD = hm.dag.entryAfter ["linkGeneration"] (
let
autoReloadCmd = ''
${pkgs.ruby}/bin/ruby ${./systemd-activate.rb} \
Expand Down
26 changes: 17 additions & 9 deletions nix-darwin/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,25 @@ let

cfg = config.home-manager;

hmModule = types.submodule ({name, ...}: {
imports = import ../modules/modules.nix { inherit lib pkgs; };
hmModule = types.submoduleWith {
specialArgs = { lib = extendedLib; };
modules = [(
{name, ...}: {
imports = import ../modules/modules.nix {
inherit pkgs;
lib = extendedLib;
};

config = {
submoduleSupport.enable = true;
submoduleSupport.externalPackageInstall = cfg.useUserPackages;
config = {
submoduleSupport.enable = true;
submoduleSupport.externalPackageInstall = cfg.useUserPackages;

home.username = config.users.users.${name}.name;
home.homeDirectory = config.users.users.${name}.home;
};
});
home.username = config.users.users.${name}.name;
home.homeDirectory = config.users.users.${name}.home;
};
}
)];
};

in

Expand Down
Loading

0 comments on commit 8b26499

Please sign in to comment.