From 7265ef755a71c6ea25e77ab6d0afa21aa12b3cfd Mon Sep 17 00:00:00 2001 From: Robert Helgesson Date: Sat, 14 Oct 2017 20:56:02 +0200 Subject: [PATCH] Add experimental NixOS module --- default.nix | 2 + modules/default.nix | 64 +---------------------- modules/home-environment.nix | 10 ++-- modules/modules.nix | 85 +++++++++++++++++++++++++++++++ modules/programs/home-manager.nix | 2 +- modules/systemd.nix | 2 + nixos/default.nix | 42 +++++++++++++++ 7 files changed, 141 insertions(+), 66 deletions(-) create mode 100644 modules/modules.nix create mode 100644 nixos/default.nix diff --git a/default.nix b/default.nix index b9eedd47..5e9e02dd 100644 --- a/default.nix +++ b/default.nix @@ -11,4 +11,6 @@ rec { "home-manager-install" { propagatedBuildInputs = [ home-manager ]; } ""; + + nixos = import ./nixos; } diff --git a/modules/default.nix b/modules/default.nix index 4cba27ff..5ca0a288 100644 --- a/modules/default.nix +++ b/modules/default.nix @@ -10,60 +10,6 @@ with lib; let - modules = [ - ./files.nix - ./home-environment.nix - ./manual.nix - ./misc/fontconfig.nix - ./misc/gtk.nix - ./misc/news.nix - ./misc/nixpkgs.nix - ./misc/pam.nix - ./programs/bash.nix - ./programs/beets.nix - ./programs/browserpass.nix - ./programs/command-not-found/command-not-found.nix - ./programs/eclipse.nix - ./programs/emacs.nix - ./programs/feh.nix - ./programs/firefox.nix - ./programs/git.nix - ./programs/gnome-terminal.nix - ./programs/home-manager.nix - ./programs/htop.nix - ./programs/info.nix - ./programs/lesspipe.nix - ./programs/man.nix - ./programs/rofi.nix - ./programs/ssh.nix - ./programs/termite.nix - ./programs/texlive.nix - ./programs/vim.nix - ./programs/zsh.nix - ./services/blueman-applet.nix - ./services/compton.nix - ./services/dunst.nix - ./services/gnome-keyring.nix - ./services/gpg-agent.nix - ./services/keepassx.nix - ./services/network-manager-applet.nix - ./services/owncloud-client.nix - ./services/polybar.nix - ./services/random-background.nix - ./services/redshift.nix - ./services/screen-locker.nix - ./services/syncthing.nix - ./services/taffybar.nix - ./services/tahoe-lafs.nix - ./services/udiskie.nix - ./services/xscreensaver.nix - ./systemd.nix - ./xresources.nix - ./xsession.nix - - - ]; - collectFailed = cfg: map (x: x.message) (filter (x: !x.assertion) cfg.assertions); @@ -73,15 +19,9 @@ let in fold f res res.config.warnings; - pkgsModule = { - config._module.args.baseModules = modules; - config._module.args.pkgs = lib.mkDefault pkgs; - config._module.check = check; - config.nixpkgs.system = mkDefault pkgs.system; - }; - rawModule = lib.evalModules { - modules = [ configuration ] ++ modules ++ [ pkgsModule ]; + modules = [ configuration ] + ++ (import ./modules.nix { inherit check lib pkgs; }); }; module = showWarnings ( diff --git a/modules/home-environment.nix b/modules/home-environment.nix index 2c33ba47..38af9b85 100644 --- a/modules/home-environment.nix +++ b/modules/home-environment.nix @@ -234,9 +234,13 @@ in # script's "check" and the "write" phases. home.activation.writeBoundary = dagEntryAnywhere ""; - home.activation.installPackages = dagEntryAfter ["writeBoundary"] '' - $DRY_RUN_CMD nix-env -i ${cfg.path} - ''; + # Install packages to the user environment. This is a no-op if + # Home Manager is used as a NixOS module. + home.activation.installPackages = dagEntryAfter ["writeBoundary"] ( + optionalString (!config.nixosSubmodule) '' + $DRY_RUN_CMD nix-env -i ${cfg.path} + '' + ); home.activationPackage = let diff --git a/modules/modules.nix b/modules/modules.nix new file mode 100644 index 00000000..3dfb1134 --- /dev/null +++ b/modules/modules.nix @@ -0,0 +1,85 @@ +{ pkgs +, lib + + # Whether to enable module type checking. +, check ? true + + # Whether these modules are inside a NixOS submodule. +, nixosSubmodule ? false +}: + +with lib; + +let + + modules = [ + ./files.nix + ./home-environment.nix + ./manual.nix + ./misc/fontconfig.nix + ./misc/gtk.nix + ./misc/news.nix + ./misc/nixpkgs.nix + ./misc/pam.nix + ./programs/bash.nix + ./programs/beets.nix + ./programs/browserpass.nix + ./programs/command-not-found/command-not-found.nix + ./programs/eclipse.nix + ./programs/emacs.nix + ./programs/feh.nix + ./programs/firefox.nix + ./programs/git.nix + ./programs/gnome-terminal.nix + ./programs/home-manager.nix + ./programs/htop.nix + ./programs/info.nix + ./programs/lesspipe.nix + ./programs/man.nix + ./programs/rofi.nix + ./programs/ssh.nix + ./programs/termite.nix + ./programs/texlive.nix + ./programs/vim.nix + ./programs/zsh.nix + ./services/blueman-applet.nix + ./services/compton.nix + ./services/dunst.nix + ./services/gnome-keyring.nix + ./services/gpg-agent.nix + ./services/keepassx.nix + ./services/network-manager-applet.nix + ./services/owncloud-client.nix + ./services/polybar.nix + ./services/random-background.nix + ./services/redshift.nix + ./services/screen-locker.nix + ./services/syncthing.nix + ./services/taffybar.nix + ./services/tahoe-lafs.nix + ./services/udiskie.nix + ./services/xscreensaver.nix + ./systemd.nix + ./xresources.nix + ./xsession.nix + + + ]; + + pkgsModule = { + options.nixosSubmodule = mkOption { + type = types.bool; + internal = true; + readOnly = true; + }; + + config._module.args.baseModules = modules; + config._module.args.pkgs = lib.mkDefault pkgs; + config._module.check = check; + config.nixosSubmodule = nixosSubmodule; + config.nixpkgs.system = mkDefault pkgs.system; + }; + +in + + map import modules ++ [ pkgsModule ] diff --git a/modules/programs/home-manager.nix b/modules/programs/home-manager.nix index 2fac0bce..08b4ba01 100644 --- a/modules/programs/home-manager.nix +++ b/modules/programs/home-manager.nix @@ -45,7 +45,7 @@ in }; }; - config = mkIf cfg.enable { + config = mkIf (cfg.enable && !config.nixosSubmodule) { assertions = [{ assertion = cfg.path == null || cfg.modulesPath == null; message = "Cannot simultaneously use " diff --git a/modules/systemd.nix b/modules/systemd.nix index 06630477..ea9e07bf 100644 --- a/modules/systemd.nix +++ b/modules/systemd.nix @@ -117,7 +117,9 @@ in ++ (buildServices "timer" cfg.timers) ); + }) + (mkIf (pkgs.stdenv.isLinux && !config.nixosSubmodule) { home.activation.reloadSystemD = dagEntryAfter ["linkGeneration"] '' function systemdPostReload() { local workDir diff --git a/nixos/default.nix b/nixos/default.nix new file mode 100644 index 00000000..781ab824 --- /dev/null +++ b/nixos/default.nix @@ -0,0 +1,42 @@ +{ config, lib, pkgs, ... }: + +with lib; + +let + + cfg = config.home-manager; + + hmModule = types.submodule ( + import ../modules/modules.nix { + inherit lib pkgs; + nixosSubmodule = true; + } + ); + + activateUser = username: usercfg: '' + echo Activating home-manager configuration for ${username} + ${pkgs.sudo}/bin/sudo -u ${username} ${usercfg.home.activationPackage}/activate + ''; + +in + +{ + options = { + home-manager.users = mkOption { + type = types.attrsOf hmModule; + default = {}; + description = '' + Per-user Home Manager configuration. + ''; + }; + }; + + config = { + system.activationScripts.home-manager = + stringAfter [ "users" ] ( + concatStringsSep "\n" ( + mapAttrsToList activateUser cfg.users)); + + users.users = mapAttrs (n: v: { packages = v.home.packages; } ) cfg.users; + }; +}