Devenv: nixpkgs.mkShell.override alternative

Hi all, @domenkozar,

thank you for the awesome devenv!
I have question which I’ve been struggling to answer on my own: given devenv, is it possible to override stdenv?

The Context

I’ve got a flakified env for building swift projects (e.g. this one):

{
  outputs = { self, nixpkgs, flake-utils, }:
    flake-utils.lib.eachDefaultSystem (system:
      let
        pkgs = nixpkgs.legacyPackages.${system};
      in
      {
        # https://github.com/NixOS/nixpkgs/issues/242779#issuecomment-1732558769
        devShells.default = pkgs.mkShell.override { stdenv = pkgs.swift.stdenv; } {
          buildInputs = with pkgs;[
            swift
            swiftpm
            swiftPackages.Foundation
            darwin.apple_sdk.frameworks.AppKit
          ];

          # https://github.com/NixOS/nix/issues/6677
          shellHook = ''
            export PS1='\n\[\033[1;32m\][nix-shell:\w]\$\[\033[0m\] '
          '';
        };
      });
}

as per swift on nixos can't compile hello world: error: undefined reference to '__dso_handle' · Issue #242779 · NixOS/nixpkgs · GitHub, stdenv overriding is required, otherwise things fall apart (e.g. Foundation not being found).

The issue

I can’t properly translate stdenv overriding to devenv concepts (replaceStdenv is a last resort and doesn’t count):

{
  inputs = {
    nixpkgs.url = "nixpkgs/nixpkgs-unstable";
    devenv.url = "github:cachix/devenv";
  };
  nixConfig = {
    extra-trusted-public-keys = "devenv.cachix.org-1:w1cLUi8dv3hnoSPGAuibQv+f9TZLr6cv/Hm9XgU50cw=";
    extra-substituters = "https://devenv.cachix.org";
  };
  outputs = { self, nixpkgs, devenv, flake-utils, }@inputs:
    flake-utils.lib.eachDefaultSystem (system:
      let
        # fully rebuilds the toolchain - not a replacement for nixpkgs.mkShell.override
        pkgs = import nixpkgs {
          inherit system;
          config.replaceStdenv = {pkgs}: pkgs.swift.stdenv;
        };
      in
      {
        devShells.default = devenv.lib.mkShell {
          inherit inputs pkgs;
          modules = [
            ({ pkgs, config, ... }: {
              languages.nix.enable = true;

              pre-commit.hooks = {
                nixpkgs-fmt.enable = true;
                yamllint.enable = true;
              };


              # https://github.com/NixOS/nix/issues/6677
              enterShell = ''
                export PS1='\n\[\033[1;32m\][nix-shell:\w]\$\[\033[0m\] '
              '';

              packages = with pkgs;[
                swift
                swiftpm
                swiftPackages.Foundation
                darwin.apple_sdk.frameworks.AppKit
              ];
            })
          ];
        };
      });
}

Hacking around

Digging thru sources has lead me to the referred devshell’s mkNakedShell. devenv’s (re)-implementation declares stdenv as input parameter, which confused me, as the let expression which follows, redeclares it?

It made me doubt if it actually would’ve worked, if I had figured out a less intrusive way to solve my problem (which I have not).

Disclaimer: that’s just noob’s attempt to get a grasp on things, so everything stated here has a potential to be the opposite of the truth :slight_smile:

Thanks a lot!
Peter

2 Likes

Hey Peter,

You’re right that we need to implement this properly on the devenv side, could you open an issue for it?

We’d need a way to specify what stdenv is used before any of the modules get called, likely in devenv.yaml

Was this resolved? Couldn’t find an issue also…

1 Like

See [Feature Request:] Overriding the stdenv not yet possible · Issue #567 · cachix/devenv · GitHub, please let me know if that covers your use case.

1 Like

Can you give Allow overriding stdenv by domenkozar · Pull Request #1092 · cachix/devenv · GitHub a try and tell me if that fits your use case?

Can you give Allow overriding stdenv by domenkozar · Pull Request #1092 · cachix/devenv · GitHub a try and tell me if that fits your use case?

Hi,

Tested it and it seems to work just fine!

if you would like to check yourself:

nix develop github:ink-splatters/devenv-stdenv-override-test --impure --accept-flake-config

I also mentioned a few small issues I experienced when going “from zero to hero”, that is, creating devenv project from scratch.

Feel free to check: GitHub - ink-splatters/devenv-stdenv-override-test: small project to test devenv's stdenv override support

TL;DR:

The only annoying issue is: creating the project with:

nix flake init --template github:cachix/devenv

and merely overriding stdenv (I chose llvmPackages_17.stdenv) makes it compile llvm :dizzy_face:

however I just used nixpkgs-unstable and it worked well :slightly_smiling_face:

Thank you!

1 Like

I’m tracking all upstreamable patches in Bump · Issue #1 · cachix/devenv-nixpkgs · GitHub and hopefully we’ll be back to plain nixpkgs soon.