How to specify host and build platform in a nix package (cross compiling in Nix)

I am new to Nix and struggling to grasp cross compilation concept with Nix. I understand that one can access the attributes stdenv.hostPlatform and buildPlatform in a Nix package and accordingly select libraries and tools.

However I am confused about 2 things:

  • I am creating a nix package → how do I specify what is the host platform and build platform for this package? By default Nix picks the system on which user is building as “build” platform and “host” platform defaults to “build” and “target” platform defaults to “host”. However as a package creator, how do I tell Nix about the build and host platforms.
  • How to give a build and host platform that is not known by Nix? Is it possible to define my own custom platform in my Nix package?

I read the nix documentation (Nixpkgs 23.11 manual | Nix & NixOS), however was not able to answer the above 2 questions.

Any help or pointers would be appreciated.

Few trials I did:

My original code

{cmake, stdenv, gcc, coreutils, bar, git, doxygen, lib}:

stdenv.mkDerivation rec {

  pname = "foo";
  
  version = "1.0";

  src = ./foo;

  buildInputs = [ bar ];

  nativeBuildInputs = [ cmake doxygen ];

  strictDeps = true;

  configurePhase = ''
    cmake -G "Unix Makefiles" -DCMAKE_TOOLCHAIN_FILE=$src/tools/toolchains/mytoolchain.cmake .
  '';

  installPhase = ''
    make install
    mkdir -p $out;
    cp -R lib $out;
    cp -R include $out;
    cp -R bin $out;
  '';

  dontFixup = true;
}

I changed or added “system” attribute in the above code and Nix do not rebuild the package.

  system = "mycpu-myos";

I was assuming that Nix will detect that it is a new system or build platfrom; which is unknown and should fail; becasue the current system on which user is trying to build is not the “system” mentioned in the nix expression.

I know, I am missing very basic understanding of concept here. But could not figure out, what.

Hi,

I’m curious about the following piece of information from your question. I can’t find this when reading through the manual: