From e2699c2ceadb9aee5f832c8517e7ec162a6e270c Mon Sep 17 00:00:00 2001 From: Gabriel Arazas Date: Mon, 29 Nov 2021 17:56:24 +0800 Subject: [PATCH] Update home-manager modules and related configs --- flake.lock | 6 +-- flake.nix | 71 +++++++++++++++++++------------- hosts/README.adoc | 12 ++++++ hosts/ni/default.nix | 1 + modules/README.adoc | 4 +- modules/users.nix | 8 ++-- users/README.adoc | 33 +++++++++++++++ users/foo-dogsquared/default.nix | 11 ++++- users/modules/alacritty.nix | 16 +++++++ users/modules/dev.nix | 34 +++++++++++++++ users/modules/i18n.nix | 21 ++++++++++ 11 files changed, 180 insertions(+), 37 deletions(-) create mode 100644 hosts/README.adoc create mode 100644 users/modules/alacritty.nix create mode 100644 users/modules/dev.nix create mode 100644 users/modules/i18n.nix diff --git a/flake.lock b/flake.lock index 09a0bd1d..ee0a7243 100644 --- a/flake.lock +++ b/flake.lock @@ -42,11 +42,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1638097282, - "narHash": "sha256-EXCzj9b8X/lqDPJapxZThIOKL5ASbpsJZ+8L1LnY1ig=", + "lastModified": 1638129630, + "narHash": "sha256-vaYC1DhrR1nao2rQ8fUf4PNhBLfSeCi2LVrDXTxUu1Y=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "78cb77b29d37a9663e05b61abb4fa09465da4b70", + "rev": "9f25a8ac3a985ba213f775b19750efefdecc9974", "type": "github" }, "original": { diff --git a/flake.nix b/flake.nix index 7c80e2e2..4b922ac7 100644 --- a/flake.nix +++ b/flake.nix @@ -12,24 +12,38 @@ outputs = inputs@{ self, nixpkgs, home-manager, ... }: let - libExtended = nixpkgs.lib.extend - (final: prev: (import ./lib { inherit inputs; lib = final; })); + # All the target systems. + systems = [ + "x86_64-linux" + "i686-linux" + "x86_64-darwin" + "aarch64-linux" + "armv6l-linux" + "armv7l-linux" + ]; + + forAllSystems = f: nixpkgs.lib.genAttrs systems (system: f system); + libExtended = nixpkgs.lib.extend (final: prev: + (import ./lib { + inherit inputs; + lib = final; + })); hostDefaultConfig = { # Stallman-senpai will be disappointed. nixpkgs.config.allowUnfree = true; - + # We live in a Unicode world and dominantly English in technical fields so we'll # have to go with it. i18n.defaultLocale = "en_US.UTF-8"; - + # Sane config for the package manager. nix.gc = { automatic = true; dates = "monthly"; options = "--delete-older-than 2w"; }; - + # TODO: Remove this after nix-command and flakes has been considered stable. # # Since we're using flakes to make this possible, we need it. @@ -39,37 +53,38 @@ ''; }; - userDefaultConfig = { - system = "x86_64-linux"; - }; + userDefaultConfig = { system = "x86_64-linux"; }; in { # Exposes only my library with the custom functions to make it easier to include in other flakes. - lib = import ./lib { inherit inputs; lib = nixpkgs.lib; }; + lib = import ./lib { + inherit inputs; + lib = nixpkgs.lib; + }; # A list of NixOS configurations from the `./hosts` folder. # It also has some sensible default configurations. - nixosConfigurations = - libExtended.mapAttrs (host: path: libExtended.mkHost path hostDefaultConfig) (libExtended.filesToAttr ./hosts); + nixosConfigurations = libExtended.mapAttrs + (host: path: libExtended.mkHost path hostDefaultConfig) + (libExtended.filesToAttr ./hosts); # We're going to make our custom modules available for our flake. Whether # or not this is a good thing is debatable, I just want to test it. - nixosModules = - libExtended.mapAttrs (_: path: import path) (libExtended.filesToAttr ./modules); + nixosModules = libExtended.mapAttrs (_: path: import path) + (libExtended.filesToAttr ./modules); + + homeConfigurations = let + excludedModules = [ + "config" # The common user-specific configurations. + "modules" # User-specific Nix modules. + ]; + in libExtended.mapAttrs (user: path: + home-manager.lib.homeManagerConfiguration { + system = "x86_64-linux"; + configuration = import path; + homeDirectory = "/home/${user}"; + username = user; + }) (libExtended.filterAttrs (n: v: !libExtended.elem n excludedModules) + (libExtended.filesToAttr ./users)); - homeConfigurations = - let - excludedModules = [ - "config" # The common user-specific configurations. - "modules" # User-specific Nix modules. - ]; - in - libExtended.mapAttrs (user: path: - home-manager.lib.homeManagerConfiguration { - system = "x86_64-linux"; - configuration = import path; - homeDirectory = "/home/${user}"; - username = user; - }) - (libExtended.filterAttrs (n: v: !libExtended.elem n excludedModules) (libExtended.filesToAttr ./users)); }; } diff --git a/hosts/README.adoc b/hosts/README.adoc new file mode 100644 index 00000000..a1397646 --- /dev/null +++ b/hosts/README.adoc @@ -0,0 +1,12 @@ += Host-specific configuration +:toc: + +These are configurations that are specific to a machine (e.g., desktop, servers, VM, containers). +Ideally, it should be made minimal as much as possible considering you also have to manage your users. + +For managing users, there are multiple ways to manage them with this config: + +* The usual `users.users.${user}` from system configuration (see `man configuration.nix.5`). + +* Modularize them (see link:../users/README.adoc[User-specific configuration] for more information). +If you have user-specific configs in set, you have to manage them with link:../modules/users.nix[`modules.users.users`] which is my implementation of managing users (e.g., `modules.users.users = [ "foo-dogsquared" ]`). diff --git a/hosts/ni/default.nix b/hosts/ni/default.nix index 6c26b868..1f61bcb9 100644 --- a/hosts/ni/default.nix +++ b/hosts/ni/default.nix @@ -21,6 +21,7 @@ dev = { enable = true; shell.enable = true; + virtualization.enable = true; }; editors = { emacs.enable = true; diff --git a/modules/README.adoc b/modules/README.adoc index d1bd2046..e9cedc47 100644 --- a/modules/README.adoc +++ b/modules/README.adoc @@ -42,8 +42,8 @@ modules/ dev = path/to/dev.nix; editors = path/to/editors.nix; specific = { - borg = path/to/specific/borg.nix - prometheus = path/to/specific/prometheus.nix + borg = path/to/specific/borg.nix; + prometheus = path/to/specific/prometheus.nix; }; themes = path/to/themes; # Since it has a 'default.nix' detected, we're using it instead. users = path/to/users.nix; diff --git a/modules/users.nix b/modules/users.nix index 7733e4ba..4f66b94c 100644 --- a/modules/users.nix +++ b/modules/users.nix @@ -10,18 +10,20 @@ let mkUser = user: path: let + userModule = import path; defaultConfig = { home.username = user; home.homeDirectory = "/home/${user}"; + + xdg.enable = true; }; in - { + { users.users.${user} = { isNormalUser = true; extraGroups = [ "wheel" ]; }; - - home-manager.users.${user} = defaultConfig // import path; + home-manager.users.${user} = userModule // defaultConfig; }; in { diff --git a/users/README.adoc b/users/README.adoc index 5fcab3fe..1120b07d 100644 --- a/users/README.adoc +++ b/users/README.adoc @@ -19,3 +19,36 @@ Here's an example of a sample user config placed in `users/hello.nix`. ---- This is to be imported to `homeManagerConfiguration` in the flake outputs and when indicated from `config.modules.users.users` (e.g., `modules.users.users = [ "hello" ];`). + + + + +== Modules + +There are also user modules (in link:./modules[`./modules`]) that are imported to use with home-manager, allowing you to extend it as you wish. +It works just like link:https://github.com/nix-community/home-manager/tree/master/modules[home-manager modules]. + +For easier identification, it should be stored with `modules` as the top-level attribute (e.g., `modules.alacritty`, `modules.i18n`). + +Here's an example user module that simply ports my Alacritty config. + +[source, nix] +---- +{ config, options, lib, pkgs, ... }: + +let + cfg = config.modules.alacritty; +in +{ + options.modules.alacritty.enable = lib.mkEnableOption "Ports my Alacritty configuration."; + + config = lib.mkIf cfg.enable { + home.packages = with pkgs; [ alacritty ]; + xdg.configFile."alacritty" = { + source = ../config/alacritty + recursive = true; + }; + }; +} +---- + diff --git a/users/foo-dogsquared/default.nix b/users/foo-dogsquared/default.nix index ccc9079e..110a092e 100644 --- a/users/foo-dogsquared/default.nix +++ b/users/foo-dogsquared/default.nix @@ -1,11 +1,20 @@ { config, options, lib, pkgs, ... }: { - programs.direnv.enable = true; + fonts.fontconfig.enable = true; + + # My specific usual stuff. programs.git = { enable = true; lfs.enable = true; userName = "foo-dogsquared"; userEmail = "foo.dogsquared@gmail.com"; }; + + # My custom modules. + modules = { + alacritty.enable = true; + i18n.enable = true; + dev.enable = true; + }; } diff --git a/users/modules/alacritty.nix b/users/modules/alacritty.nix new file mode 100644 index 00000000..3a691b65 --- /dev/null +++ b/users/modules/alacritty.nix @@ -0,0 +1,16 @@ +{ config, options, lib, pkgs, ... }: + +let + cfg = config.modules.alacritty; +in +{ + options.modules.alacritty.enable = lib.mkEnableOption "Enable Alacritty config"; + + config = lib.mkIf cfg.enable { + home.packages = with pkgs; [ alacritty ]; + xdg.configFile."alacritty" = { + source = ../config/alacritty/alacritty.yml; + recursive = true; + }; + }; +} diff --git a/users/modules/dev.nix b/users/modules/dev.nix new file mode 100644 index 00000000..b85015b7 --- /dev/null +++ b/users/modules/dev.nix @@ -0,0 +1,34 @@ +{ config, options, lib, pkgs, ... }: + +let + cfg = config.modules.dev; +in + { + options.modules.dev = { + enable = lib.mkEnableOption "Enable my user-specific development setup."; + shell.enable = lib.mkEnableOption "Configures my shell of choice."; + }; + + config = lib.mkIf cfg.enable (lib.mkMerge [ + ({ + home.packages = with pkgs; [ + neovim # My text editor of choice. + lazygit # Git interface for the lazy. + fzf # A fuzzy finder that enables fuzzy finding not furry finding, a common misconception. + gopass # An improved version of the password manager for hipsters. + + # Coreutils replacement. + fd # Oh nice, a more reliable `find`. + ripgrep # On nice, a more reliable `grep`. + exa # Oh nice, a shinier `ls`. + bat # dog > bat > cat + ]; + + programs.direnv = { + enable = true; + nix-direnv.enable = true; + }; + programs.zoxide.enable = true; + }) + ]); +} diff --git a/users/modules/i18n.nix b/users/modules/i18n.nix new file mode 100644 index 00000000..f90129e4 --- /dev/null +++ b/users/modules/i18n.nix @@ -0,0 +1,21 @@ +{ config, options, lib, pkgs, ... }: + +let + cfg = config.modules.i18n; +in + { + options.modules.i18n.enable = lib.mkEnableOption "Enable fcitx5 as input method engine."; + + config = lib.mkIf cfg.enable { + i18n.inputMethod = { + enabled = "fcitx5"; + fcitx5.addons = with pkgs; [ + fcitx5-gtk # Add support for GTK-based programs. + libsForQt5.fcitx5-qt # Add support for QT-based programs. + fcitx5-lua # Add Lua support. + fcitx5-rime # Chinese input addon. + fcitx5-mozc # Japanese input addon. + ]; + }; + }; + }