How to remove a file from a fetchGit?

Is there a way to remove a config file from a fetchGit? I’d like to replace it with my own config. So far I’ve tried adding postFetch and prePatch, but neither of those are correct. Does anyone have any suggestions?

      ".config/yazelix".source = builtins.fetchGit {
        url = "https://github.com/luccahuguet/yazelix.git";
        rev = "d5dade229a1fef57567516e57cfc392cfb7ba266";
        prePatch = ''
          rm $HOME/.config/yazelix/zellij/config.kdl
        '';
      };

fetchGit is not a derivation, you meant to use pkgs.fetchgit or better pkgs.fetchFromGitHub

So there’s no way to exclude a file without making it a derivation?

https://nixos.org/manual/nixpkgs/unstable/#sec-functions-library-fileset

I’ve been trying to make my first derivation for the last couple of days, as suggested. At this point I’m still trying to get the github files to pull in as is, without modifying the config file, but I’m stuck with this unclear error message:

error: builder for '/nix/store/y8b2s9brarj5nqhknbrn3qqw6998j031-yazelix.drv' failed with exit code 2;
       last 10 log lines:
       > 	--norc
       > 	--posix
       > 	--pretty-print
       > 	--rcfile
       > 	--restricted
       > 	--verbose
       > 	--version
       > Shell options:
       > 	-ilrsD or -c command or -O shopt_option		(invocation only)
       > 	-abefhkmnptuvxBCEHPT or -o option
       For full logs, run 'nix log /nix/store/y8b2s9brarj5nqhknbrn3qqw6998j031-yazelix.drv'.

Here’s my derivation:

  { pkgs ? import <nixpkgs> {} }:

  pkgs.stdenv.mkDerivation {
    name = "yazelix";
    src = pkgs.fetchFromGitHub {
      owner = "luccahuguet";
      repo = "yazelix";
      rev = "1093c18c22d9d9858f4cf5813bd9aa7578660af8";
      sha256 = "0c1haix90hsh0pbv31f5nvkraqbxzp6a1a3cz1cmvd3zckr2m8zv";
    };
    args = [ "--output" "~/.config/yazelix/" ];
    buildCommand = ''
      touch $out
    '';
    prePatch = ''
    '';
    configurePhase = ''
    '';
    buildPhase = ''
    '';
    installPhase = ''
    '';
  }

I never said to use mkDerivation, though you can if you want…
I said to use pkgs.fetchFromGitHub which is a derivation itself.

  xdg.configFile.yazelix.source = pkgs.fetchFromGitHub {
    owner = "luccahuguet";
    repo = "yazelix";
    rev = "d5dade229a1fef57567516e57cfc392cfb7ba266";
    hash = ""; # build and get the hash from the error
    postFetch = ''
      rm $out/zellij/config.kdl
    '';
  };

Or if you still want to use fetchGit (inherently slower for larger repos, and unparallelisable since it’s part of eval) then use lib.fileset.* to filter out files that you don’t want.

I’m assuming I just put that in my home.nix after

{ config, pkgs, ... }:
let variable = import ../variables.nix;
in {

and not under home.file in my home.nix, where I originally had it? If that’s correct, it’s not deleting the config.kdl file for some reason.

I don’t think that’s possible, fileset is only for local files, not fetched ones!

1 Like

Fair, I didn’t test that, thanks for the correction.

For some reason nothing in the postFetch seems to be doing anything. Anyone able to replicate this?

  xdg.configFile.yazelix.source = pkgs.fetchFromGitHub {
    owner = "luccahuguet";
    repo = "yazelix";
    rev = "1093c18c22d9d9858f4cf5813bd9aa7578660af8";
    hash = "sha256-+6Mq8mR/tF1Z+GyooMz9fWGV57bFhbHXBVBDkHpUMDA="; # build and get the hash from the error
    postFetch = ''
      rm $out/zellij/config.kdl
      cp ./apps/yazelix/config.kdl $out/zellij/config.kdl
    '';
  };

Empty the hash.​​​​​​
Also I’m not sure where you believe you’re copying from…

Couldn’t you apply it to the output path of fetchgit?

Nope:

You can also check the reason for this :slight_smile:

1 Like

@waffle8946 thanks for the “removing the hash” tip - it revealed an error around that path, like you suspected. So I tried changing that path to an absolute path:

cp /home/guttermonk/.config/nixos/home/apps/yazelix/config.kdl $out/zellij/config.kdl

And it’s still mad, but I’m not sure why. The file’s right there:

Here’s the error if that helps:

cp: cannot stat '/home/guttermonk/.config/nixos/home/apps/yazelix/config.kdl': No such file or directory

I’m not 100% sure what your local layout looks like but try modifying your postFetch as follows, wrapping the filename of the local relative file in ${...}:

    postFetch = ''
      rm $out/zellij/config.kdl
      cp ${./apps/yazelix/config.kdl} $out/zellij/config.kdl
    '';

That should get the right filename for the relative path regardless of how exactly things are set up.

2 Likes

You can’t do that, builds are sandboxed.
@jchw gave one possible solution, which works because that path will be copied to the nix store first, thereby allowing the builder to access it.

That worked! Thanks everyone for the help. My system is now fully declarative - that was the last piece of the puzzle. Got my colemak-dh keybindings and everything themed :pinched_fingers:. This community is awesome.

Here’s the full solution for anyone that stumbles up on this thread, thanks to @waffle8946 and @jchw:

  xdg.configFile.yazelix.source = pkgs.fetchFromGitHub {
    owner = "luccahuguet";
    repo = "yazelix";
    rev = "1093c18c22d9d9858f4cf5813bd9aa7578660af8";
    hash = ""; # build and get the hash from the error
    postFetch = ''
      rm $out/zellij/config.kdl
      cp ${./apps/yazelix/config.kdl} $out/zellij/config.kdl
    '';
  };
3 Likes