Nix build to generate reusable environment

Hi All

I am new to nix. Here is what I want to do:

  1. Specify a list of packages for setting up environment for say Java (tools like gradle, JDK, maven etc).
  2. Then I want to do a nix build and get the symlink to the resulting nix store.
  3. I want to use that link for later purpose whenever I want to do Java dev.

Here is what I have tried:

  1. Read about flakes. I understand nix flakes is the recommended way.
  2. Tried devshells within flake. It does provide me with a shell of the Java tools when I do nix develop
  3. But I dont want a shell right away. Rather I want to keep the Java environment somewhere, and load it on demand (when I want to do Java dev). Likewise, I may want to switch to say a NodeJS env.

Below is the nix flake I wrote. Its pretty basic, but I get an error when I do a nix build.

  description = "Java dependencies";

  inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.05";

  outputs = { self, nixpkgs }: {
      packages.x86_64-linux.default = 
        with import nixpkgs { system = "x86_64-linux"; };
        stdenv.mkDerivation {
          name = "java";
          src = self;
          buildInputs = [

The error when I do a nix build is:

builder for '/nix/store/6kqidjixlygm71sk84lg15bcrpgbxnm2-java.drv' failed to produce output path for output 'out' at '/nix/store/6kqidjixlygm71sk84lg15bcrpgbxnm2-java.drv.chroot/nix/store/cqss3a495m9ihs27yzh0gzvr878ixma5-java'

I have used devshells and it works - but I dont want a shell immediately. Rather I want to load the java env based on the symlink produced by nix build… Alternatively can I use nix profile for this? I am thinking I can maybe have a separate profile for Java environment, and separate for Nodejs and so on…
Someone experienced with nixpkgs please help (I am on a NonNix OS)…


@meain Any idea on how I can achieve the above?

Maybe I don’t quite understand what you want to do but from what I gathered I would recommend to use the nix shell. In your case the devShell within the flake. I use the devShell in combination with direnv and the use flake command within the .envrc file.

1 Like

Yes, nix build --profile java