Using patchelf in flakes to set the rpath

I am trying to build the following two derivations: is_odd which contains a shared library and main which dlopens that shared library. I have this working with the classic nix-build by passing is_odd from a top level flake into main so that I can execute this in mkDerivation:

postFixup = ''
    patchelf $out/bin/main --add-rpath ${is_odd.out}/lib
'';

However, this does not seem possible with flakes since I cannot import is_odd into main nor can I pass it in as an argument. This is what I have tried so far:

Top level flake:

{
  description = "Top level flake";

  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.11";
    main.url =  "path:./main";
    is_odd.url =  "path:./is_odd";
  };

  outputs = { self, main, is_odd, nixpkgs }: {

    packages.x86_64-linux.main = main.packages.x86_64-linux.main;
    packages.x86_64-linux.is_odd = is_odd.packages.x86_64-linux.is_odd;
    defaultPackage.x86_64-linux = let
      pkgs = import nixpkgs {
        system = "x86_64-linux";
      };
      in pkgs.symlinkJoin {
        name = "assets";
        paths = [
          main.packages.x86_64-linux.main
          is_odd.packages.x86_64-linux.is_odd
        ];
      };
  };
}

is_odd flake:

{
  inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.11";
  
  outputs = { self, nixpkgs }: {
    packages.x86_64-linux.is_odd =
      let
        pkgs = import nixpkgs {
          system = "x86_64-linux";
        };
        fs = pkgs.lib.fileset;
        sourceFiles = fs.unions [
          ./CMakeLists.txt
          (fs.fileFilter (file: file.hasExt "c" || file.hasExt "h") ./src)
        ];
      in pkgs.stdenv.mkDerivation {
        pname = "is_odd";
        version = "1.0.0";
        src = fs.toSource {
          root = ./.;
          fileset = sourceFiles;
        };
        nativeBuildInputs = with pkgs; [
          cmake
        ];
      };
  };
}

main flake (currently fails because due to using ../ in input path):

{
  inputs = {
    is_odd.url =  "path:../is_odd";
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.11";
  };
  
  outputs = { self, nixpkgs, is_odd }: {
    packages.x86_64-linux.main =
      let
        pkgs = import nixpkgs {
          system = "x86_64-linux";
        };
        fs = pkgs.lib.fileset;
        sourceFiles = fs.unions [
          ./CMakeLists.txt
          (fs.fileFilter (file: file.hasExt "cxx" || file.hasExt "h") ./src)
        ];
      in pkgs.stdenv.mkDerivation {
        pname = "main";
        version = "1.0.0";
        src = fs.toSource {
          root = ./.;
          fileset = sourceFiles;
        };
        nativeBuildInputs = with pkgs; [
          cmake
        ];
        postFixup = ''
          readelf -d $out/bin/main | grep PATH
          patchelf $out/bin/main --add-rpath ${is_odd.out}/lib
          readelf -d $out/bin/main | grep PATH
        '';
      };
  };
}

How can I solve this problem? Also any advice on how to make my flakes more readable or idiomatic would really be appreciated!

Please post the error message you are getting. Otherwise we can only apply best effort guessing.

flakes forbid this by default because it is impure and not reproducible. You can pass --impure to temporarily allow this.

Thanks for the reply @Sandro, however, it is not really an error message that I am having trouble with. The problem is that I do not know how to reference is_odd from the main flake, that is, so that I can call patchelf like this:

patchelf $out/bin/main --add-rpath ${is_odd.out}/lib

${is_odd} refers to the flake itself, not the package in it, you will need to use:

${is_odd.packages.x86_64-linux.is_odd}
# ^ Flake input                ^ Actual package

That probably would have been the next issue I ran into after fixing the first. However, I am still stuck at how to reference is_odd from main which are both flakes in the same repository.