Nix develop --command is ignored

Hi,

I recently started migrating my development environment to Nix flakes. Previously, I relied on nix-shell with the following shell.nix:

# This commit is from NixOS 24.05 (28-06-2024)
with (import (fetchTarball https://github.com/NixOS/nixpkgs/archive/89c49874fb15f4124bf71ca5f42a04f2ee5825fd.tar.gz) { config.allowUnfree = true; });

let
  dotnet_sdk = (with dotnetCorePackages; combinePackages [
    # Install all the .NET versions needed here...
    sdk_8_0
  ]);
in
  mkShell {
    name = "dotnet-env";

    packages = [
      # .NET SDK
      dotnet_sdk
      # Run PowerShell scripts, which are sometimes included in NuGet packages like Playwright
      powershell
      # Timezones
      tzdata
      # Locales
      glibcLocales
    ];

    shellHook = ''
      # Bunch of `exports` commands...
      (...)
    '';

    # Without this, there are warnings about LANG, LC_ALL and locales.
    # Many tests fail due those warnings showing up in test outputs too...
    # This solution is from: https://gist.github.com/aabs/fba5cd1a8038fb84a46909250d34a5c1
    LOCALE_ARCHIVE = "${glibcLocales}/lib/locale/locale-archive";
  }

As for my Nix flakes setup, here it is flake.nix:

{
  description = ".NET development environment based on the Filesystem Hierarchy Standard (FHS)";
  # https://en.wikipedia.org/wiki/Filesystem_Hierarchy_Standard

  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
  };

  outputs = { nixpkgs, ... } @ inputs:
  let
    system = "x86_64-linux";
    pkgs = nixpkgs.legacyPackages.${system};

    dotnet_sdk = (with pkgs.dotnetCorePackages; combinePackages [
      # Install all the .NET SDK versions needed here...
      sdk_8_0
    ]);
  in
  {
    # Create development shell based on the Filesystem Hierarchy Standard (FHS) with a set of
    # standard packages based on the list maintained by the appimagetools package
    #
    # buildFHSEnv -> https://nixos.org/manual/nixpkgs/stable/#sec-fhs-environments
    #
    # The packages included in appimagetools.defaultFhsEnvArgs are:
    # https://github.com/NixOS/nixpkgs/blob/fd6a510ec7e84ccd7f38c2ad9a55a18bf076f738/pkgs/build-support/appimage/default.nix#L72-L208
    devShells.${system}.default = (pkgs.buildFHSEnv (pkgs.appimageTools.defaultFhsEnvArgs // {
          name = "dotnet-development-environment";
          # Packages installed in the development shell
          targetPkgs = pkgs: with pkgs; [
            # .NET SDK
            dotnet_sdk
            # Run PowerShell scripts, which are sometimes included in NuGet packages like Playwright
            powershell
            # Timezones
            tzdata
            # Locales
            glibcLocales
          ];
          # Commands to be executed in the development shell
          profile = ''
            # Bunch of `export` commands...
            (...) 
          '';
        })).env;
  };
}

To launch my IDE, I would run nix-shell --run 'nohup IDE_COMMAND &' which I am now trying to replicate with nix develop --command 'nohup IDE_COMMAND &', but I’m simply taken into the interactive shell and the command is ignored.

What am I doing wrong here? Thank you very much for your help!

I believe that I’ve found others with this exact issue: nix-shell --command doesn't work for FHS environments · Issue #1569 · NixOS/nix · GitHub

nix develop doesn’t start a shell, it runs exec on the --command, so you can’t use & in there. Instead of a string, you should also just give it the command and args.

Either of these should work:

# Fork off nix, keep it running as a parent process
nohup nix develop --command IDE_COMMAND &
# Fork off `nix`, and then keep your IDE running
nix develop --command nohup IDE_COMMAND &

I have no idea why you end up in a shell when you specify --command with a string including spaces though. Nix should error out telling you the command does not exist. Are you sure that’s what’d happening?

Yes, I really end up in a shell when I specify --command. It’s the same issue as the one I linked in my second comment from this topic.

The second command you suggested is the same as the one I tried, except without the quotes surrounding nohup IDE_COMMAND &. I tried out your first suggestion too, but it didn’t work.

This is all caused by the devShell being built with buildFHSEnv.

nix-shell (which people are reporting broken) works totally differently, so I thought there’s a chance this is different. nix-shell will invoke bashInteractive with whatever string you give it (it’s called shell, after all).

Yeah, that’s my point, the quotes should break the invocation. This is because nix-shell asks bash to parse the string, while nix develop just gives it to the kernel. Those quotes make all the difference.

This is why it’s a bit surprising to me that you end up dropping into a shell at all. I guess something in the fhsUserEnv script ends up creating a shell and not interacting well with the rest of the functionality. I’d go digging into the internals of that script to see where it’s going wrong to debug further, this is probably not too hard to fix?