Proper way of applying patch to system managed via flake

If I read this correctly, thats IFD?

I would be happy if there was a blessed and officially supported way to apply patches to any flake inputs that would not cause IFD.

Of course I understand that this would require an additional evaluation phase if patches have been used.

Still I think flakes would improve overall if thats supported

2 Likes

Yes, pretty sure it is IFD. I only do this on my flake for my NixOS systems.

1 Like

I wonder if that could actually be a nix builtin, some kind of syntax around inputs. “I want to patch my input” is a generic problem, even if nixpkgs’ size makes it more frequent, and nix needs git support to pull the inputs already, so why not have it patch the input before evaluation?

2 Likes

edolstra already has a WIP PR which makes this a builtin, although it’s slightly more limited as you have to have the patch files in the flake rather than fetching.

https://github.com/NixOS/nix/pull/6530

3 Likes

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

3 Likes

The results of builtins.fetchTree is not a derivation, and therefore, can be used to avoid IFD in this situation, at least until the new patch builtin is finally merged.