From 7c8941a764da327ed8cb1653e4f56fb3983d0ef8 Mon Sep 17 00:00:00 2001 From: Gabriel Fontes Date: Tue, 8 Nov 2022 17:59:31 -0300 Subject: [PATCH] reorganize modules and overlays This update makes using flake (self or external) modules and overlays more explicit, by setting nixpkgs options directly in the config, instead of the flake. To help import your own overlays and modules, the `outputs` attribute is now available on your configs. --- README.md | 19 ++------------- minimal/flake.lock | 24 +++++++++--------- minimal/home-manager/home.nix | 26 ++++++++++++++++++-- minimal/nixos/configuration.nix | 33 ++++++++++++++++++------- standard/flake.lock | 24 +++++++++--------- standard/flake.nix | 21 ++++++---------- standard/home-manager/home.nix | 37 +++++++++++++++++++++++++--- standard/nixos/configuration.nix | 42 ++++++++++++++++++++++++-------- standard/nixpkgs-config.nix | 10 -------- standard/nixpkgs.nix | 8 ++++++ standard/pkgs/default.nix | 7 +++--- standard/shell.nix | 24 ++++++------------ 12 files changed, 167 insertions(+), 108 deletions(-) delete mode 100644 standard/nixpkgs-config.nix create mode 100644 standard/nixpkgs.nix diff --git a/README.md b/README.md index 670462a..9c8537f 100644 --- a/README.md +++ b/README.md @@ -202,9 +202,9 @@ Found some outdated package on nixpkgs you need the latest version of? Perhaps you want to apply a patch to fix a behaviour you don't like? Nix makes it easy and manageble with overlays! -Use the `overlay/default.nix` file for this. +Use the `overlays/default.nix` file for this. -If you're creating patches, you can keep them on the `overlay` folder as well. +If you're creating patches, you can keep them on the `overlays` folder as well. See [the wiki article](https://nixos.wiki/wiki/Overlays) to see how it all works. @@ -228,21 +228,6 @@ Please [let me know](https://github.com/Misterio77/nix-starter-config/issues) any questions or issues you face with these templates, so I can add more info here! -## I'm trying to set nixpkgs.config options (e.g. allowUnfree), but they won't work! - -We instantiate nixpkgs and pass it to NixOS, to make the flake a bit simpler -and ensure both configs have the same nixpkgs instance, this has the drawback -of breaking nixpkgs configuration through in your NixOS config files. - -This is a design choice I made based on the `homeManagerConfiguration` -interface, that requires a `pkgs` instance anyway. If you prefer to set them -modularly, you'll have to remove the `pkgs` argument. If you use overlays, -you'll have to pass them into your configuration (through specialArgs) or set -`nixpkgs.overlays` right on the flake; in this case, you might prefer to use -[home-manager as a NixOS -module](https://nix-community.github.io/home-manager/index.html#sec-flakes-nixos-module) -and set `home-manager.useGlobalPkgs = true`. - ## Nix says my repo files don't exist, even though they do! Nix flakes only see files that git is currently tracked, so just `git add .` diff --git a/minimal/flake.lock b/minimal/flake.lock index 60d72e9..0fbb0a5 100644 --- a/minimal/flake.lock +++ b/minimal/flake.lock @@ -2,11 +2,11 @@ "nodes": { "hardware": { "locked": { - "lastModified": 1667585378, - "narHash": "sha256-cvOwucrjBaAkaGk3FunG+MQiwiSBeIVTtO5n/YavpC0=", + "lastModified": 1667768008, + "narHash": "sha256-PGbX0s2hhXGnZDFVE6UIhPSOf5YegpWs5dUXpT/14F0=", "owner": "nixos", "repo": "nixos-hardware", - "rev": "6b35a59c19ddbbeb229fcd1d3dcd422dcc0fa927", + "rev": "f6483e0def85efb9c1e884efbaff45a5e7aabb34", "type": "github" }, "original": { @@ -23,11 +23,11 @@ "utils": "utils" }, "locked": { - "lastModified": 1667574732, - "narHash": "sha256-73TVk3uSQOja6Q/5OuNcpcqwo6+SMzJPRtYAjU0rBx4=", + "lastModified": 1667898954, + "narHash": "sha256-VqHVeoxcOl9M6yQ+LV3yTWMb0h5Rl5yixn9PCY/MJJo=", "owner": "nix-community", "repo": "home-manager", - "rev": "b764068a506c6f70dba998efa0e7fcb99cb4deb2", + "rev": "d20e3d070c78271356a2d5d73c01f1de94586087", "type": "github" }, "original": { @@ -38,11 +38,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1667482890, - "narHash": "sha256-pua0jp87iwN7NBY5/ypx0s9L9CG49Ju/NI4wGwurHc4=", + "lastModified": 1667811565, + "narHash": "sha256-HYml7RdQPQ7X13VNe2CoDMqmifsXbt4ACTKxHRKQE3Q=", "owner": "nixos", "repo": "nixpkgs", - "rev": "a2a777538d971c6b01c6e54af89ddd6567c055e8", + "rev": "667e5581d16745bcda791300ae7e2d73f49fff25", "type": "github" }, "original": { @@ -61,11 +61,11 @@ }, "utils": { "locked": { - "lastModified": 1667077288, - "narHash": "sha256-bdC8sFNDpT0HK74u9fUkpbf1MEzVYJ+ka7NXCdgBoaA=", + "lastModified": 1667395993, + "narHash": "sha256-nuEHfE/LcWyuSWnS8t12N1wc105Qtau+/OdUAjtQ0rA=", "owner": "numtide", "repo": "flake-utils", - "rev": "6ee9ebb6b1ee695d2cacc4faa053a7b9baa76817", + "rev": "5aed5285a952e0b949eb3ba02c12fa4fcfef535f", "type": "github" }, "original": { diff --git a/minimal/home-manager/home.nix b/minimal/home-manager/home.nix index bafc020..58fc881 100644 --- a/minimal/home-manager/home.nix +++ b/minimal/home-manager/home.nix @@ -2,13 +2,35 @@ # Use this to configure your home environment (it replaces ~/.config/nixpkgs/home.nix) { inputs, lib, config, pkgs, ... }: { + # You can import other home-manager modules here imports = [ - # If you want to use home-manager modules from other flakes (such as nix-colors), use something like: + # If you want to use home-manager modules from other flakes (such as nix-colors): # inputs.nix-colors.homeManagerModule - # Feel free to split up your configuration and import pieces of it here. + # You can also split up your configuration and import pieces of it here: + # ./nvim.nix ]; + nixpkgs = { + # You can add overlays here + overlays = [ + # If you want to use overlays exported from other flakes: + # neovim-nightly-overlay.overlays.default + + # Or define it inline, for example: + # (final: prev: { + # hi = final.hello.overrideAttrs (oldAttrs: { + # patches = [ ./change-hello-to-hi.patch ]; + # }); + # }) + ]; + # Configure your nixpkgs instance + config = { + # Disable if you don't want unfree packages + allowUnfree = true; + }; + }; + # TODO: Set your username home = { username = "your-username"; diff --git a/minimal/nixos/configuration.nix b/minimal/nixos/configuration.nix index 2997965..f4f824c 100644 --- a/minimal/nixos/configuration.nix +++ b/minimal/nixos/configuration.nix @@ -2,22 +2,39 @@ # Use this to configure your system environment (it replaces /etc/nixos/configuration.nix) { inputs, lib, config, pkgs, ... }: { - + # You can import other NixOS modules here imports = [ - # If you want to use modules from other flakes (such as nixos-hardware), use something like: + # If you want to use modules from other flakes (such as nixos-hardware): # inputs.hardware.nixosModules.common-cpu-amd # inputs.hardware.nixosModules.common-ssd - # It's strongly recommended you take a look at - # https://github.com/nixos/nixos-hardware - # and import modules relevant to your hardware. + # You can also split up your configuration and import pieces of it here: + # ./users.nix # Import your generated (nixos-generate-config) hardware configuration ./hardware-configuration.nix - - # You can also split up your configuration and import pieces of it here. ]; + nixpkgs = { + # You can add overlays here + overlays = [ + # If you want to use overlays exported from other flakes: + # neovim-nightly-overlay.overlays.default + + # Or define it inline, for example: + # (final: prev: { + # hi = final.hello.overrideAttrs (oldAttrs: { + # patches = [ ./change-hello-to-hi.patch ]; + # }); + # }) + ]; + # Configure your nixpkgs instance + config = { + # Disable if you don't want unfree packages + allowUnfree = true; + }; + }; + nix = { # This will add each flake input as a registry # To make nix3 commands consistent with your flake @@ -26,9 +43,7 @@ # This will additionally add your inputs to the system's legacy channels # Making legacy nix commands consistent as well, awesome! nixPath = lib.mapAttrsToList (key: value: "${key}=${value.to.path}") config.nix.registry; - }; - nix = { settings = { # Enable flakes and new 'nix' command experimental-features = "nix-command flakes"; diff --git a/standard/flake.lock b/standard/flake.lock index 60d72e9..0fbb0a5 100644 --- a/standard/flake.lock +++ b/standard/flake.lock @@ -2,11 +2,11 @@ "nodes": { "hardware": { "locked": { - "lastModified": 1667585378, - "narHash": "sha256-cvOwucrjBaAkaGk3FunG+MQiwiSBeIVTtO5n/YavpC0=", + "lastModified": 1667768008, + "narHash": "sha256-PGbX0s2hhXGnZDFVE6UIhPSOf5YegpWs5dUXpT/14F0=", "owner": "nixos", "repo": "nixos-hardware", - "rev": "6b35a59c19ddbbeb229fcd1d3dcd422dcc0fa927", + "rev": "f6483e0def85efb9c1e884efbaff45a5e7aabb34", "type": "github" }, "original": { @@ -23,11 +23,11 @@ "utils": "utils" }, "locked": { - "lastModified": 1667574732, - "narHash": "sha256-73TVk3uSQOja6Q/5OuNcpcqwo6+SMzJPRtYAjU0rBx4=", + "lastModified": 1667898954, + "narHash": "sha256-VqHVeoxcOl9M6yQ+LV3yTWMb0h5Rl5yixn9PCY/MJJo=", "owner": "nix-community", "repo": "home-manager", - "rev": "b764068a506c6f70dba998efa0e7fcb99cb4deb2", + "rev": "d20e3d070c78271356a2d5d73c01f1de94586087", "type": "github" }, "original": { @@ -38,11 +38,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1667482890, - "narHash": "sha256-pua0jp87iwN7NBY5/ypx0s9L9CG49Ju/NI4wGwurHc4=", + "lastModified": 1667811565, + "narHash": "sha256-HYml7RdQPQ7X13VNe2CoDMqmifsXbt4ACTKxHRKQE3Q=", "owner": "nixos", "repo": "nixpkgs", - "rev": "a2a777538d971c6b01c6e54af89ddd6567c055e8", + "rev": "667e5581d16745bcda791300ae7e2d73f49fff25", "type": "github" }, "original": { @@ -61,11 +61,11 @@ }, "utils": { "locked": { - "lastModified": 1667077288, - "narHash": "sha256-bdC8sFNDpT0HK74u9fUkpbf1MEzVYJ+ka7NXCdgBoaA=", + "lastModified": 1667395993, + "narHash": "sha256-nuEHfE/LcWyuSWnS8t12N1wc105Qtau+/OdUAjtQ0rA=", "owner": "numtide", "repo": "flake-utils", - "rev": "6ee9ebb6b1ee695d2cacc4faa053a7b9baa76817", + "rev": "5aed5285a952e0b949eb3ba02c12fa4fcfef535f", "type": "github" }, "original": { diff --git a/standard/flake.nix b/standard/flake.nix index 97aead5..4cf1842 100644 --- a/standard/flake.nix +++ b/standard/flake.nix @@ -17,8 +17,9 @@ # nix-colors.url = "github:misterio77/nix-colors"; }; - outputs = { nixpkgs, home-manager, ... }@inputs: + outputs = { self, nixpkgs, home-manager, ... }@inputs: let + inherit (self) outputs; forAllSystems = nixpkgs.lib.genAttrs [ "aarch64-linux" "i686-linux" @@ -32,15 +33,13 @@ # Acessible through 'nix build', 'nix shell', etc packages = forAllSystems (system: let pkgs = nixpkgs.legacyPackages.${system}; - in - import ./pkgs { inherit pkgs; } + in import ./pkgs { inherit pkgs; } ); # Devshell for bootstrapping # Acessible through 'nix develop' or 'nix-shell' (legacy) devShells = forAllSystems (system: let pkgs = nixpkgs.legacyPackages.${system}; - in - import ./shell.nix { inherit pkgs; } + in import ./shell.nix { inherit pkgs; } ); # Your custom packages and modifications, exported as overlays @@ -55,12 +54,10 @@ nixosConfigurations = { # FIXME replace with your hostname your-hostname = nixpkgs.lib.nixosSystem { - specialArgs = { inherit inputs; }; # Pass flake inputs to our config - modules = (builtins.attrValues nixosModules) ++ [ + specialArgs = { inherit inputs outputs; }; + modules = [ # > Our main nixos configuration file < ./nixos/configuration.nix - # Our common nixpkgs config (unfree, overlays, etc) - (import ./nixpkgs-config.nix { inherit overlays; }) ]; }; }; @@ -69,12 +66,10 @@ # FIXME replace with your username@hostname "your-username@your-hostname" = home-manager.lib.homeManagerConfiguration { pkgs = nixpkgs.legacyPackages.x86_64-linux; # Home-manager requires 'pkgs' instance - extraSpecialArgs = { inherit inputs; }; # Pass flake inputs to our config - modules = (builtins.attrValues homeManagerModules) ++ [ + extraSpecialArgs = { inherit inputs outputs; }; + modules = [ # > Our main home-manager configuration file < ./home-manager/home.nix - # Our common nixpkgs config (unfree, overlays, etc) - (import ./nixpkgs-config.nix { inherit overlays; }) ]; }; }; diff --git a/standard/home-manager/home.nix b/standard/home-manager/home.nix index bafc020..02c1df1 100644 --- a/standard/home-manager/home.nix +++ b/standard/home-manager/home.nix @@ -1,14 +1,43 @@ # This is your home-manager configuration file # Use this to configure your home environment (it replaces ~/.config/nixpkgs/home.nix) -{ inputs, lib, config, pkgs, ... }: { +{ inputs, outputs, lib, config, pkgs, ... }: { + # You can import other home-manager modules here imports = [ - # If you want to use home-manager modules from other flakes (such as nix-colors), use something like: - # inputs.nix-colors.homeManagerModule + # If you want to use modules your own flake exports (from modules/home-manager): + # outputs.homeManagerModules.example - # Feel free to split up your configuration and import pieces of it here. + # Or modules exported from other flakes (such as nix-colors): + # inputs.nix-colors.homeManagerModules.default + + # You can also split up your configuration and import pieces of it here: + # ./nvim.nix ]; + nixpkgs = { + # You can add overlays here + overlays = [ + # If you want to use overlays your own flake exports (from overlays dir): + # outputs.overlays.modifications + # outputs.overlays.additions + + # Or overlays exported from other flakes: + # neovim-nightly-overlay.overlays.default + + # Or define it inline, for example: + # (final: prev: { + # hi = final.hello.overrideAttrs (oldAttrs: { + # patches = [ ./change-hello-to-hi.patch ]; + # }); + # }) + ]; + # Configure your nixpkgs instance + config = { + # Disable if you don't want unfree packages + allowUnfree = true; + }; + }; + # TODO: Set your username home = { username = "your-username"; diff --git a/standard/nixos/configuration.nix b/standard/nixos/configuration.nix index 27ec31f..7db9279 100644 --- a/standard/nixos/configuration.nix +++ b/standard/nixos/configuration.nix @@ -1,23 +1,47 @@ # This is your system's configuration file. # Use this to configure your system environment (it replaces /etc/nixos/configuration.nix) -{ inputs, lib, config, pkgs, ... }: { - +{ inputs, outputs, lib, config, pkgs, ... }: { + # You can import other NixOS modules here imports = [ - # If you want to use modules from other flakes (such as nixos-hardware), use something like: + # If you want to use modules your own flake exports (from modules/nixos): + # outputs.nixosModules.example + + # Or modules from other flakes (such as nixos-hardware): # inputs.hardware.nixosModules.common-cpu-amd # inputs.hardware.nixosModules.common-ssd - # It's strongly recommended you take a look at - # https://github.com/nixos/nixos-hardware - # and import modules relevant to your hardware. + # You can also split up your configuration and import pieces of it here: + # ./users.nix # Import your generated (nixos-generate-config) hardware configuration ./hardware-configuration.nix - - # You can also split up your configuration and import pieces of it here. ]; + nixpkgs = { + # You can add overlays here + overlays = [ + # If you want to use overlays your own flake exports (from overlays dir): + # outputs.overlays.modifications + # outputs.overlays.additions + + # Or overlays exported from other flakes: + # neovim-nightly-overlay.overlays.default + + # Or define it inline, for example: + # (final: prev: { + # hi = final.hello.overrideAttrs (oldAttrs: { + # patches = [ ./change-hello-to-hi.patch ]; + # }); + # }) + ]; + # Configure your nixpkgs instance + config = { + # Disable if you don't want unfree packages + allowUnfree = true; + }; + }; + nix = { # This will add each flake input as a registry # To make nix3 commands consistent with your flake @@ -26,9 +50,7 @@ # This will additionally add your inputs to the system's legacy channels # Making legacy nix commands consistent as well, awesome! nixPath = lib.mapAttrsToList (key: value: "${key}=${value.to.path}") config.nix.registry; - }; - nix = { settings = { # Enable flakes and new 'nix' command experimental-features = "nix-command flakes"; diff --git a/standard/nixpkgs-config.nix b/standard/nixpkgs-config.nix deleted file mode 100644 index 84957b9..0000000 --- a/standard/nixpkgs-config.nix +++ /dev/null @@ -1,10 +0,0 @@ -{ overlays, ... }: -{ - nixpkgs = { - config = { - allowUnfree = true; - }; - # Overlays is an attrset, convert to a list - overlays = builtins.attrValues overlays; - }; -} diff --git a/standard/nixpkgs.nix b/standard/nixpkgs.nix new file mode 100644 index 0000000..041de40 --- /dev/null +++ b/standard/nixpkgs.nix @@ -0,0 +1,8 @@ +# A nixpkgs instance that is grabbed from the pinned nixpkgs commit in the lock file +# This is useful to avoid using channels when using legacy nix commands +let lock = (builtins.fromJSON (builtins.readFile ./flake.lock)).nodes.nixpkgs.locked; +in +import (fetchTarball { + url = "https://github.com/nixos/nixpkgs/archive/${lock.rev}.tar.gz"; + sha256 = lock.narHash; +}) diff --git a/standard/pkgs/default.nix b/standard/pkgs/default.nix index 55e428d..8c5e910 100644 --- a/standard/pkgs/default.nix +++ b/standard/pkgs/default.nix @@ -1,5 +1,6 @@ -# When you add custom packages, list them here -# These are similar to nixpkgs packages -{ pkgs }: { +# Custom packages, that can be defined similarly to ones from nixpkgs +# You can build them using 'nix build .#example' or (legacy) 'nix-build -A example' + +{ pkgs ? (import ../nixpkgs.nix) { } }: { # example = pkgs.callPackage ./example { }; } diff --git a/standard/shell.nix b/standard/shell.nix index 0ab6972..9525915 100644 --- a/standard/shell.nix +++ b/standard/shell.nix @@ -1,18 +1,10 @@ # Shell for bootstrapping flake-enabled nix and home-manager -{ pkgs ? let - # If pkgs is not defined, instantiate nixpkgs from locked commit - lock = (builtins.fromJSON (builtins.readFile ./flake.lock)).nodes.nixpkgs.locked; - nixpkgs = fetchTarball { - url = "https://github.com/nixos/nixpkgs/archive/${lock.rev}.tar.gz"; - sha256 = lock.narHash; - }; - system = builtins.currentSystem; - overlays = [ ]; # Explicit blank overlay to avoid interference - in - import nixpkgs { inherit system overlays; } -, ... -}: pkgs.mkShell { - # Enable experimental features without having to specify the argument - NIX_CONFIG = "experimental-features = nix-command flakes"; - nativeBuildInputs = with pkgs; [ nix home-manager git ]; +# You can enter it through 'nix develop' or (legacy) 'nix-shell' + +{ pkgs ? (import ./nixpkgs.nix) { } }: { + default = pkgs.mkShell { + # Enable experimental features without having to specify the argument + NIX_CONFIG = "experimental-features = nix-command flakes"; + nativeBuildInputs = with pkgs; [ nix home-manager git ]; + }; }