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.
This commit is contained in:
Gabriel Fontes 2022-11-08 17:59:31 -03:00
parent 4a4bc73702
commit 7c8941a764
12 changed files with 167 additions and 108 deletions

View file

@ -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 you want to apply a patch to fix a behaviour you don't like? Nix makes it easy
and manageble with overlays! 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 See [the wiki article](https://nixos.wiki/wiki/Overlays) to see how it all
works. 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 any questions or issues you face with these templates, so I can add more info
here! 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 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 .` Nix flakes only see files that git is currently tracked, so just `git add .`

View file

@ -2,11 +2,11 @@
"nodes": { "nodes": {
"hardware": { "hardware": {
"locked": { "locked": {
"lastModified": 1667585378, "lastModified": 1667768008,
"narHash": "sha256-cvOwucrjBaAkaGk3FunG+MQiwiSBeIVTtO5n/YavpC0=", "narHash": "sha256-PGbX0s2hhXGnZDFVE6UIhPSOf5YegpWs5dUXpT/14F0=",
"owner": "nixos", "owner": "nixos",
"repo": "nixos-hardware", "repo": "nixos-hardware",
"rev": "6b35a59c19ddbbeb229fcd1d3dcd422dcc0fa927", "rev": "f6483e0def85efb9c1e884efbaff45a5e7aabb34",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -23,11 +23,11 @@
"utils": "utils" "utils": "utils"
}, },
"locked": { "locked": {
"lastModified": 1667574732, "lastModified": 1667898954,
"narHash": "sha256-73TVk3uSQOja6Q/5OuNcpcqwo6+SMzJPRtYAjU0rBx4=", "narHash": "sha256-VqHVeoxcOl9M6yQ+LV3yTWMb0h5Rl5yixn9PCY/MJJo=",
"owner": "nix-community", "owner": "nix-community",
"repo": "home-manager", "repo": "home-manager",
"rev": "b764068a506c6f70dba998efa0e7fcb99cb4deb2", "rev": "d20e3d070c78271356a2d5d73c01f1de94586087",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -38,11 +38,11 @@
}, },
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1667482890, "lastModified": 1667811565,
"narHash": "sha256-pua0jp87iwN7NBY5/ypx0s9L9CG49Ju/NI4wGwurHc4=", "narHash": "sha256-HYml7RdQPQ7X13VNe2CoDMqmifsXbt4ACTKxHRKQE3Q=",
"owner": "nixos", "owner": "nixos",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "a2a777538d971c6b01c6e54af89ddd6567c055e8", "rev": "667e5581d16745bcda791300ae7e2d73f49fff25",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -61,11 +61,11 @@
}, },
"utils": { "utils": {
"locked": { "locked": {
"lastModified": 1667077288, "lastModified": 1667395993,
"narHash": "sha256-bdC8sFNDpT0HK74u9fUkpbf1MEzVYJ+ka7NXCdgBoaA=", "narHash": "sha256-nuEHfE/LcWyuSWnS8t12N1wc105Qtau+/OdUAjtQ0rA=",
"owner": "numtide", "owner": "numtide",
"repo": "flake-utils", "repo": "flake-utils",
"rev": "6ee9ebb6b1ee695d2cacc4faa053a7b9baa76817", "rev": "5aed5285a952e0b949eb3ba02c12fa4fcfef535f",
"type": "github" "type": "github"
}, },
"original": { "original": {

View file

@ -2,13 +2,35 @@
# Use this to configure your home environment (it replaces ~/.config/nixpkgs/home.nix) # Use this to configure your home environment (it replaces ~/.config/nixpkgs/home.nix)
{ inputs, lib, config, pkgs, ... }: { { inputs, lib, config, pkgs, ... }: {
# You can import other home-manager modules here
imports = [ 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 # 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 # TODO: Set your username
home = { home = {
username = "your-username"; username = "your-username";

View file

@ -2,22 +2,39 @@
# Use this to configure your system environment (it replaces /etc/nixos/configuration.nix) # Use this to configure your system environment (it replaces /etc/nixos/configuration.nix)
{ inputs, lib, config, pkgs, ... }: { { inputs, lib, config, pkgs, ... }: {
# You can import other NixOS modules here
imports = [ 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-cpu-amd
# inputs.hardware.nixosModules.common-ssd # inputs.hardware.nixosModules.common-ssd
# It's strongly recommended you take a look at # You can also split up your configuration and import pieces of it here:
# https://github.com/nixos/nixos-hardware # ./users.nix
# and import modules relevant to your hardware.
# Import your generated (nixos-generate-config) hardware configuration # Import your generated (nixos-generate-config) hardware configuration
./hardware-configuration.nix ./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 = { nix = {
# This will add each flake input as a registry # This will add each flake input as a registry
# To make nix3 commands consistent with your flake # To make nix3 commands consistent with your flake
@ -26,9 +43,7 @@
# This will additionally add your inputs to the system's legacy channels # This will additionally add your inputs to the system's legacy channels
# Making legacy nix commands consistent as well, awesome! # Making legacy nix commands consistent as well, awesome!
nixPath = lib.mapAttrsToList (key: value: "${key}=${value.to.path}") config.nix.registry; nixPath = lib.mapAttrsToList (key: value: "${key}=${value.to.path}") config.nix.registry;
};
nix = {
settings = { settings = {
# Enable flakes and new 'nix' command # Enable flakes and new 'nix' command
experimental-features = "nix-command flakes"; experimental-features = "nix-command flakes";

View file

@ -2,11 +2,11 @@
"nodes": { "nodes": {
"hardware": { "hardware": {
"locked": { "locked": {
"lastModified": 1667585378, "lastModified": 1667768008,
"narHash": "sha256-cvOwucrjBaAkaGk3FunG+MQiwiSBeIVTtO5n/YavpC0=", "narHash": "sha256-PGbX0s2hhXGnZDFVE6UIhPSOf5YegpWs5dUXpT/14F0=",
"owner": "nixos", "owner": "nixos",
"repo": "nixos-hardware", "repo": "nixos-hardware",
"rev": "6b35a59c19ddbbeb229fcd1d3dcd422dcc0fa927", "rev": "f6483e0def85efb9c1e884efbaff45a5e7aabb34",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -23,11 +23,11 @@
"utils": "utils" "utils": "utils"
}, },
"locked": { "locked": {
"lastModified": 1667574732, "lastModified": 1667898954,
"narHash": "sha256-73TVk3uSQOja6Q/5OuNcpcqwo6+SMzJPRtYAjU0rBx4=", "narHash": "sha256-VqHVeoxcOl9M6yQ+LV3yTWMb0h5Rl5yixn9PCY/MJJo=",
"owner": "nix-community", "owner": "nix-community",
"repo": "home-manager", "repo": "home-manager",
"rev": "b764068a506c6f70dba998efa0e7fcb99cb4deb2", "rev": "d20e3d070c78271356a2d5d73c01f1de94586087",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -38,11 +38,11 @@
}, },
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1667482890, "lastModified": 1667811565,
"narHash": "sha256-pua0jp87iwN7NBY5/ypx0s9L9CG49Ju/NI4wGwurHc4=", "narHash": "sha256-HYml7RdQPQ7X13VNe2CoDMqmifsXbt4ACTKxHRKQE3Q=",
"owner": "nixos", "owner": "nixos",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "a2a777538d971c6b01c6e54af89ddd6567c055e8", "rev": "667e5581d16745bcda791300ae7e2d73f49fff25",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -61,11 +61,11 @@
}, },
"utils": { "utils": {
"locked": { "locked": {
"lastModified": 1667077288, "lastModified": 1667395993,
"narHash": "sha256-bdC8sFNDpT0HK74u9fUkpbf1MEzVYJ+ka7NXCdgBoaA=", "narHash": "sha256-nuEHfE/LcWyuSWnS8t12N1wc105Qtau+/OdUAjtQ0rA=",
"owner": "numtide", "owner": "numtide",
"repo": "flake-utils", "repo": "flake-utils",
"rev": "6ee9ebb6b1ee695d2cacc4faa053a7b9baa76817", "rev": "5aed5285a952e0b949eb3ba02c12fa4fcfef535f",
"type": "github" "type": "github"
}, },
"original": { "original": {

View file

@ -17,8 +17,9 @@
# nix-colors.url = "github:misterio77/nix-colors"; # nix-colors.url = "github:misterio77/nix-colors";
}; };
outputs = { nixpkgs, home-manager, ... }@inputs: outputs = { self, nixpkgs, home-manager, ... }@inputs:
let let
inherit (self) outputs;
forAllSystems = nixpkgs.lib.genAttrs [ forAllSystems = nixpkgs.lib.genAttrs [
"aarch64-linux" "aarch64-linux"
"i686-linux" "i686-linux"
@ -32,15 +33,13 @@
# Acessible through 'nix build', 'nix shell', etc # Acessible through 'nix build', 'nix shell', etc
packages = forAllSystems (system: packages = forAllSystems (system:
let pkgs = nixpkgs.legacyPackages.${system}; let pkgs = nixpkgs.legacyPackages.${system};
in in import ./pkgs { inherit pkgs; }
import ./pkgs { inherit pkgs; }
); );
# Devshell for bootstrapping # Devshell for bootstrapping
# Acessible through 'nix develop' or 'nix-shell' (legacy) # Acessible through 'nix develop' or 'nix-shell' (legacy)
devShells = forAllSystems (system: devShells = forAllSystems (system:
let pkgs = nixpkgs.legacyPackages.${system}; let pkgs = nixpkgs.legacyPackages.${system};
in in import ./shell.nix { inherit pkgs; }
import ./shell.nix { inherit pkgs; }
); );
# Your custom packages and modifications, exported as overlays # Your custom packages and modifications, exported as overlays
@ -55,12 +54,10 @@
nixosConfigurations = { nixosConfigurations = {
# FIXME replace with your hostname # FIXME replace with your hostname
your-hostname = nixpkgs.lib.nixosSystem { your-hostname = nixpkgs.lib.nixosSystem {
specialArgs = { inherit inputs; }; # Pass flake inputs to our config specialArgs = { inherit inputs outputs; };
modules = (builtins.attrValues nixosModules) ++ [ modules = [
# > Our main nixos configuration file < # > Our main nixos configuration file <
./nixos/configuration.nix ./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 # FIXME replace with your username@hostname
"your-username@your-hostname" = home-manager.lib.homeManagerConfiguration { "your-username@your-hostname" = home-manager.lib.homeManagerConfiguration {
pkgs = nixpkgs.legacyPackages.x86_64-linux; # Home-manager requires 'pkgs' instance pkgs = nixpkgs.legacyPackages.x86_64-linux; # Home-manager requires 'pkgs' instance
extraSpecialArgs = { inherit inputs; }; # Pass flake inputs to our config extraSpecialArgs = { inherit inputs outputs; };
modules = (builtins.attrValues homeManagerModules) ++ [ modules = [
# > Our main home-manager configuration file < # > Our main home-manager configuration file <
./home-manager/home.nix ./home-manager/home.nix
# Our common nixpkgs config (unfree, overlays, etc)
(import ./nixpkgs-config.nix { inherit overlays; })
]; ];
}; };
}; };

View file

@ -1,14 +1,43 @@
# This is your home-manager configuration file # This is your home-manager configuration file
# Use this to configure your home environment (it replaces ~/.config/nixpkgs/home.nix) # 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 = [ imports = [
# If you want to use home-manager modules from other flakes (such as nix-colors), use something like: # If you want to use modules your own flake exports (from modules/home-manager):
# inputs.nix-colors.homeManagerModule # 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 # TODO: Set your username
home = { home = {
username = "your-username"; username = "your-username";

View file

@ -1,23 +1,47 @@
# This is your system's configuration file. # This is your system's configuration file.
# Use this to configure your system environment (it replaces /etc/nixos/configuration.nix) # 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 = [ 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-cpu-amd
# inputs.hardware.nixosModules.common-ssd # inputs.hardware.nixosModules.common-ssd
# It's strongly recommended you take a look at # You can also split up your configuration and import pieces of it here:
# https://github.com/nixos/nixos-hardware # ./users.nix
# and import modules relevant to your hardware.
# Import your generated (nixos-generate-config) hardware configuration # Import your generated (nixos-generate-config) hardware configuration
./hardware-configuration.nix ./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 = { nix = {
# This will add each flake input as a registry # This will add each flake input as a registry
# To make nix3 commands consistent with your flake # To make nix3 commands consistent with your flake
@ -26,9 +50,7 @@
# This will additionally add your inputs to the system's legacy channels # This will additionally add your inputs to the system's legacy channels
# Making legacy nix commands consistent as well, awesome! # Making legacy nix commands consistent as well, awesome!
nixPath = lib.mapAttrsToList (key: value: "${key}=${value.to.path}") config.nix.registry; nixPath = lib.mapAttrsToList (key: value: "${key}=${value.to.path}") config.nix.registry;
};
nix = {
settings = { settings = {
# Enable flakes and new 'nix' command # Enable flakes and new 'nix' command
experimental-features = "nix-command flakes"; experimental-features = "nix-command flakes";

View file

@ -1,10 +0,0 @@
{ overlays, ... }:
{
nixpkgs = {
config = {
allowUnfree = true;
};
# Overlays is an attrset, convert to a list
overlays = builtins.attrValues overlays;
};
}

8
standard/nixpkgs.nix Normal file
View file

@ -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;
})

View file

@ -1,5 +1,6 @@
# When you add custom packages, list them here # Custom packages, that can be defined similarly to ones from nixpkgs
# These are similar to nixpkgs packages # You can build them using 'nix build .#example' or (legacy) 'nix-build -A example'
{ pkgs }: {
{ pkgs ? (import ../nixpkgs.nix) { } }: {
# example = pkgs.callPackage ./example { }; # example = pkgs.callPackage ./example { };
} }

View file

@ -1,18 +1,10 @@
# Shell for bootstrapping flake-enabled nix and home-manager # Shell for bootstrapping flake-enabled nix and home-manager
{ pkgs ? let # You can enter it through 'nix develop' or (legacy) 'nix-shell'
# If pkgs is not defined, instantiate nixpkgs from locked commit
lock = (builtins.fromJSON (builtins.readFile ./flake.lock)).nodes.nixpkgs.locked; { pkgs ? (import ./nixpkgs.nix) { } }: {
nixpkgs = fetchTarball { default = pkgs.mkShell {
url = "https://github.com/nixos/nixpkgs/archive/${lock.rev}.tar.gz"; # Enable experimental features without having to specify the argument
sha256 = lock.narHash; NIX_CONFIG = "experimental-features = nix-command flakes";
}; nativeBuildInputs = with pkgs; [ nix home-manager git ];
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 ];
} }