Read files from remote repository using nixOs

Solution 1:

Here's a short derivation that reads a file from a remote git repo that contains no Nix configuration:

with (import <nixpkgs> { });
let
  repo = fetchFromGitHub {
    owner = "nix-community";
    repo = "awesome-nix";
    rev = "c4adba38dc2ec33aa0f692cc5fcb9677b123087c";
    sha256 = "cF9sh3vrDwTh64ZkgyEuJKmmA4UhbnXw8x4cnBMeGHk=";
  };
in stdenv.mkDerivation {
  name = "count-repo-lines";
  src = repo;
  buildPhase = ''
    mkdir $out
    wc -l ./README.md > linecount
  '';
  installPhase = ''
    cp linecount $out/linecount
  '';
  system = builtins.currentSystem;
}

If it were saved to a file named count-remote-lines.nix, then the nix-build command would put the output into a local folder named result:

$ nix-build ./count-remote-lines.nix
...various log messages...

$ cat ./result/linecount 
154 ./README.md

Alternatively, to better troubleshoot the derivation's build process by stepping through it in an interactive shell, try:

$ nix-shell ./count-repo-lines.nix --pure

$ unpackPhase
unpacking source archive /nix/store/xaff1yqipbpazhp9jz22rjp7izbglzr5-source
source root is source

$ cd source
$ ls
CONTRIBUTING.md  LICENSE  README.md

The remaining build commands are documented in man nix-shell, under Examples. Also in wiki pages such as this one.