Proper way of applying patch to system managed via flake

ryantm’s approach only patches pkgs, which doesn’t work if the patch contains fix for nixosmodule for example.

Here’s attempt version to patch the whole nixpkgs for use:

      system = "x86_64-linux";

      pkgs-init = import inputs.nixpkgs { inherit system; };

      patches = [
        (pkgs-boot.fetchpatch {
          url = "https://patch-diff.githubusercontent.com/raw/NixOS/nixpkgs/pull/207758.patch";
          hash = "sha256-1bxn+U0NslCTElG+EhJe43FRf+5tIgMh7gvPKAyGe0U=";
        })
      ];

      nixpkgs-patched =
        pkgs-boot.applyPatches {
          name = "nixpkgs-patched";
          src = inputs.nixpkgs;
          inherit patches;
        };

      pkgs = import nixpkgs-patched {
        inherit system;

        config.allowUnfree = true;

        overlays = [
          emacs-overlay.overlays.default
          .....
        ];
      };

      nixpkgs = (import "${nixpkgs-patched}/flake.nix").outputs { self = inputs.self; };

The trick is the last line of the import, which would expose back a patched nixpkgs which has lib and nixosModules, so you can use the same old nixpkgs.lib.nixosSystem for example. The patched nixpkgs is a bit different than the original inputs.nixpkgs, but that’s as good as I can get it to work.

Somebody did the similar thing for darwin as well. Support flake references to patches · Issue #3920 · NixOS/nix · GitHub

4 Likes