Patching nixpkgs for testing nixos modules pr

There is a wiki page already if only packages needs a patched nixpkgs: Nixpkgs/Patching Nixpkgs - Official NixOS Wiki

for nixos module patches I came up with this solution. It currently uses flake-parts but it is trivial to refactor to use without flake-parts

{
  withSystem,
  inputs,
  ...
}:
{
  perSystem =
    { system, pkgs, ... }:
    let
      patched = toString (
        pkgs.applyPatches {
          name = "nixpkgs-patched";
          src = inputs.nixpkgs;
          patches = [
            (pkgs.fetchpatch {
              name = "grub-module-keep-booted-system-entry-option.patch";
              url = "https://github.com/NixOS/nixpkgs/pull/487895.patch";
              hash = "sha256-ys6bro8F3fmxc4yvHgB3+LbnrU7yB8c/5RQ+NZMQCEI=";
            })
            (pkgs.fetchpatch {
              name = "remove more wrappers.patch";
              url = "https://github.com/NixOS/nixpkgs/pull/477696.patch";
              hash = "sha256-vDdUMnJcmTz6HmL/uzUNsMCjKo4X9YijMYK+/MyVdiQ=";
            })
          ];
        }
      );
      nixpkgs = import "${patched}/flake.nix" // {
        outPath = patched;
      };
    in
    {
      _module.args.nixpkgs-patched =
        nixpkgs.outputs {
          self = nixpkgs;
        }
        // {
          outPath = patched;
        };
    };
  flake =
    let
      inputs0 = inputs;
    in
    let
      nixosSystem =
        { system, ... }@args:
        let
          nixpkgs = withSystem system ({ nixpkgs-patched, ... }: nixpkgs-patched);
          inputs = inputs0 // {
            inherit nixpkgs;
          };
        in
        nixpkgs.lib.nixosSystem (
          args
          // {
            specialArgs = {
              inherit inputs system;
            }
            // (args.specialArgs or { });
          }
        );
    in
........
```
3 Likes

an example with standalone home-manager with nix flake:



  outputs =
    inputs0@{
      home-manager,
      ...
    }:
    let
      the = (
        system:
        let
          pkgs0 = import inputs0.nixpkgs {
            inherit system;
          };
          nixgl-drv = pkgs0.applyPatches {
            name = "nixgl-patched";
            src = inputs0.nixgl.outPath;
            patches = with pkgs0; [
              (fetchpatch {
                name = "build on non-x86 (aarch64) platforms.patch";
                url = "https://github.com/nix-community/nixGL/pull/182.patch";
                hash = "sha256-FpHvGKbJF7lxX1WJaQVWC0VpDRxRhU4z2VNSaQ8R+eY=";
              })
              (fetchpatch {
                name = "fix warning system -> stdenv.hostPlatform.system.patch";
                url = "https://github.com/nix-community/nixGL/pull/214.patch";
                hash = "sha256-MTM4oPMIM6rZ2LNZHsOPH41EzaDI/6nEXZolcrhYVaE=";
              })
            ];
          };
          nixgl =
            (import "${nixgl-drv}/flake.nix").outputs {
              self = nixgl;
              inherit nixpkgs;
              flake-utils = inputs0.nixgl.inputs.flake-utils;
            }
            // {
              outPath = toString nixgl-drv;
            };
          nixpkgs-drv = pkgs0.applyPatches {
            name = "nixpkgs-patched";
            src = inputs0.nixpkgs.outPath;
            patches = with pkgs0; [
              (fetchpatch {
                name = "feat: add aarch64 lmstudio package.patch";
                url = "https://github.com/NixOS/nixpkgs/pull/491906.patch";
                hash = "sha256-3H8FosZT0E1pHbWVnaMaJxwjOJ9EHsjTskZcPuhdyfo=";
              })
            ];
          };
          nixpkgs =
            (import "${nixpkgs-drv}/flake.nix").outputs {
              self = nixpkgs;
            }
            // {
              outPath = toString nixpkgs-drv;
            };
          pkgs-pin = import inputs0.nixpkgs-pin {
            inherit system;
          };
          pkgs = import nixpkgs {
            inherit system;
            overlays = [
              inputs0.nur.overlays.default
              inputs0.nix-vscode-extensions.overlays.default
              (final: prev: {
                shell-gpt = pkgs-pin.shell-gpt;
              })
            ];
          };
        in
        {
          inputs = inputs0 // {
            inherit nixpkgs nixgl;
          };
          inherit pkgs;
        }
      );
    in
    {
      homeConfigurations."xxx" =
        with the "x86_64-linux";
        home-manager.lib.homeManagerConfiguration {
          inherit pkgs;
          modules = [
            ./xxx
          ];
          extraSpecialArgs = {
            inherit inputs;
          };
        };

I usually use the disabledModules option. For testing plantuml-server I have

plantuml-server-test.nix

nixpkgs-plantuml-server-branch: {pkgs, ...}: {
  disabledModules = [
    "services/web-apps/plantuml-server.nix"
  ];
  imports = [
    "${nixpkgs-plantuml-server-branch}/nixos/modules/services/web-apps/plantuml-server.nix"
  ];
  services.plantuml-server = {
    enable = true;
    listenPort = 29254;
    package = nixpkgs-plantuml-server-branch.legacyPackages.${pkgs.stdenv.hostPlatform.system}.plantuml-server;
  };
}

and in my flake.nix I have

      nixosConfigurations.bruichladdich = nixpkgs.lib.nixosSystem {
        modules = [
          #...
          (import ./hosts/bruichladdich/plantuml-server-test.nix nixpkgs-plantuml-review)

nixpkgs-plantuml-review is a flake input that points to the PR I’m reviewing.

But I’m sure there are circumstances where patches are preferable.

1 Like