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 15dda05d03
commit 8071557c31
No known key found for this signature in database
GPG key ID: 2E54EA7BFE630916
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
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 .`

View file

@ -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": {

View file

@ -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";

View file

@ -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";

View file

@ -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": {

View file

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

View file

@ -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";

View file

@ -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";

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
# 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 { };
}

View file

@ -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 {
# 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 ];
};
}