Skip to content

Instantly share code, notes, and snippets.

@DavidArsene
Last active May 11, 2026 17:19
Show Gist options
  • Select an option

  • Save DavidArsene/67cade0eb2629d875712c6283ae1557d to your computer and use it in GitHub Desktop.

Select an option

Save DavidArsene/67cade0eb2629d875712c6283ae1557d to your computer and use it in GitHub Desktop.
Wrap a nixpkgs flake input to always have allowUnfree enabled.
let
#? Get the original nixpkgs from our registry entry.
#? Directly getting "nixpkgs" doesn't work, since it
#? doesn't use the registry for resolving its inputs.
src = builtins.getFlake "nixpkgs-src";
wrapper = import ./flake.nix;
#? Explicitly call `outputs` à la flake-compat
self = wrapper.outputs { inherit self src; };
in
self
/**
Wrap a nixpkgs flake input to always have allowUnfree enabled.
Usage:
inputs = {
nixpkgs.url = "git+https://gist.github.com/DavidArsene/67cade0eb2629d875712c6283ae1557d";
nixpkgs.inputs.src.url = "github:NixOS/nixpkgs/nixos-unstable";
};
*/
{
outputs =
{ self, src }:
let
nixpkgs = src;
inherit (nixpkgs) lib;
forAllSystems = lib.genAttrs lib.systems.flakeExposed;
unfreeConfig = {
allowUnfree = true;
allowAliases = false;
};
in
nixpkgs
// {
lib = lib.extend (
final: prev: {
nixosSystem =
args:
import (nixpkgs + /nixos/lib/eval-config.nix) (
args
// {
#? As set by the original nixpkgs/flake.nix
lib = final;
system = null;
#? Usually, lib.nixosSystem doesn't go through legacyPackages,
#? relying on modules/misc/nixpkgs.nix instead. This is the root
#? cause of why nixpkgs.config is not applied to other nixpkgs
#? instances. Also, this is the only place `system` is required,
#? since it gets set to pkgs.stdenv.hostPlatform by read-only.nix
pkgs = self.legacyPackages.${args.system};
modules = args.modules ++ [
#? Ensure that pkgs is always the one from here.
nixpkgs.nixosModules.readOnlyPkgs
{
#? Required for having access to the actual nixpkgs
#? outside the flake used to build the system.
nix.registry."nixpkgs-src".flake = src;
#? And point the <nixpkgs> reference to the wrapper
nixpkgs.flake.source = self.outPath;
}
];
}
);
}
);
legacyPackages = forAllSystems (
system:
import (nixpkgs + /pkgs/top-level) {
# no builtins.currentSystem
localSystem = { inherit system; };
config =
let
date = nixpkgs.lastModifiedDate;
pretty = "${lib.substring 6 2 date}/${lib.substring 4 2 date}";
hash = lib.substring 11 32 nixpkgs.outPath;
in
builtins.warn "Using wrapped nixpkgs from ${pretty} (${hash})" unfreeConfig;
}
);
#? Convenient shortcut for using nixpkgs in e.g. the repl
pkgs = self.legacyPackages.${builtins.currentSystem};
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment