Using a result of a custom derivation in a derivation

Hello,

I packaged my CV and my blog using Nix Flakes and I want to use the result of my CV package (PDF file) in my blog. How can I do that?

Here’s the flake for my CV:

{
  description = "Nix Flake package for my CV";
  inputs.nixpkgs.url = "nixpkgs/nixos-23.11";
  outputs = { self, nixpkgs }:
    let
      pkgName = "matejascv";
      supportedSystems = [ "x86_64-linux" "x86_64-darwin" "aarch64-linux" "aarch64-darwin" ];
      forAllSystems = nixpkgs.lib.genAttrs supportedSystems;
      nixpkgsFor = forAllSystems (system: import nixpkgs { inherit system; });
      drv = {stdenv, texliveFull, ...}: stdenv.mkDerivation {
        name = pkgName;
        src = ./.;
        nativeBuildInputs = [ texliveFull ];
        buildPhase = ''
          pdflatex cv.tex
        '';
        installPhase = ''
          cp cv.pdf $out
        '';
      };
    in
    {
      overlays.default = (final: prev: {
        ${pkgName} = prev.callPackage drv {};
      });
      packages = forAllSystems (system:
        let
          pkgs = nixpkgsFor.${system};
        in {
          ${pkgName} = drv pkgs;
          default = drv pkgs;
        }
      );
      devShells = forAllSystems (system:
        let
          pkgs = nixpkgsFor.${system};
        in {
          default = pkgs.mkShell {
            buildInputs = with pkgs; [ git rsync texliveFull ];
          };
        }
      );
    };
}

Here’s the flake for my blog:

{
  description = "Nix Flake package for my blog";
  inputs.nixpkgs.url = "nixpkgs/nixos-23.11";
  outputs = { self, nixpkgs }:
    let
      pkgName = "matejasblog";
      supportedSystems = [ "x86_64-linux" "x86_64-darwin" "aarch64-linux" "aarch64-darwin" ];
      forAllSystems = nixpkgs.lib.genAttrs supportedSystems;
      nixpkgsFor = forAllSystems (system: import nixpkgs { inherit system; });
      drv = {stdenv, hugo, ...}: stdenv.mkDerivation {
        name = pkgName;
        src = ./.;
        nativeBuildInputs = [ hugo ];
        buildPhase = ''
          hugo
        '';
        installPhase = ''
          mkdir -p $out/var/www
          cp -r public $out/var/www/matejamaric.com
        '';
      };
    in
    {
      overlays.default = (final: prev: {
        ${pkgName} = prev.callPackage drv {};
      });
      packages = forAllSystems (system:
        let
          pkgs = nixpkgsFor.${system};
        in {
          ${pkgName} = drv pkgs;
          default = drv pkgs;
        }
      );
      devShells = forAllSystems (system:
        let
          pkgs = nixpkgsFor.${system};
        in {
          default = pkgs.mkShell {
            buildInputs = with pkgs; [ git rsync hugo ];
          };
        }
      );
    };
}

I would like to install the result of CV derivation (PDF file) to $out/var/www/matejamaric.com/cv.pdf

Thanks a lot for any help! :smiley:

I tried running nix build with this but it fails with /nix/store/10i1kjjq5szjn1gp6418x8bc1hswqc90-stdenv-linux/setup: line 654: source: /nix/store/cydbp9wr68x7dr198r5srf2nm0l1mq1c-matejascv: cannot execute binary file:

{
  description = "Nix Flake package for my blog";
  inputs.nixpkgs.url = "nixpkgs/nixos-23.11";
  inputs.matejascv.url = "github:/MatejaMaric/cv";
  inputs.matejascv.inputs.nixpkgs.follows = "nixpkgs";
  outputs = { self, nixpkgs, matejascv }:
    let
      pkgName = "matejasblog";
      supportedSystems = [ "x86_64-linux" "x86_64-darwin" "aarch64-linux" "aarch64-darwin" ];
      forAllSystems = nixpkgs.lib.genAttrs supportedSystems;
      nixpkgsFor = forAllSystems (system: import nixpkgs {
        inherit system;
        overlays = [ matejascv.overlays.default ];
      });
      drv = {stdenv, hugo, matejascv, ...}: stdenv.mkDerivation {
        name = pkgName;
        src = ./.;
        nativeBuildInputs = [ hugo matejascv ];
        buildPhase = ''
          hugo
        '';
        installPhase = ''
          mkdir -p $out/var/www
          cp -r public $out/var/www/matejamaric.com
          cp ${matejascv} $out/var/www/matejamaric.com/cv.pdf
        '';
      };
    in
    {
      overlays.default = (final: prev: {
        ${pkgName} = prev.callPackage drv { inherit matejascv; };
      });
      packages = forAllSystems (system:
        let
          pkgs = nixpkgsFor.${system};
        in {
          ${pkgName} = drv pkgs;
          default = drv pkgs;
        }
      );
      devShells = forAllSystems (system:
        let
          pkgs = nixpkgsFor.${system};
        in {
          default = pkgs.mkShell {
            buildInputs = with pkgs; [ git rsync hugo ];
          };
        }
      );
    };
}

I manged to find a solution. I had to remove matejascv from nativeBuildInputs so it looks like this now:

nativeBuildInputs = [ hugo ];

That fixes the cannot execute binary file error, which doesn’t make a lot of sense to me. Why does it even try to execute matejascv when I’m not calling it anywhere in the build or the install phase?


Also there’s another thing that needed fixing and that’s the following:

overlays.default = (final: prev: {
  ${pkgName} = prev.callPackage drv { inherit matejascv; };
});

Which makes sense since matejascv is just flake output and not a derivation. So, I changed it to the following:

overlays.default = (final: prev: {
  ${pkgName} = prev.callPackage drv {
    # matejascv = matejascv.packages.${prev.system}.matejascv; # same thing as the line below
    inherit (matejascv.packages.${prev.system}) matejascv;
  };
});

This overlay now works when I, for example, call it from my server configuration. But I’m not sure this is the best way to do it, there’s probably some way to stack overlays since matejascv also provides one, but I don’t know how to do it. If somebody has some tips regarding this, I would be very grateful.

In case somebody is interested, I found a better way of composing overlays, here it is:

overlays.default = nixpkgs.lib.composeManyExtensions [
  matejascv.overlays.default
  (final: prev: {
    ${pkgName} = prev.callPackage drv {};
  })
];