Bash: pop_var_context: head of shell_variables not a function context

Using the zero-to-nix flake template for Haskell, I packaged a simple Haskell program. I can run the flake just fine.

❯ nix run "git+https://codeberg.org/mhwombat/hello-flake-haskell"
Hello from Haskell!

Now I want to make the program available in a shell:

❯ cat shell.nix
with (import <nixpkgs> {});
let
  hello-flake-haskell = (builtins.getFlake git+https://codeberg.org/mhwombat/hello-flake-haskell).packages.${builtins.currentSystem}.default;
in
mkShell {
  buildInputs = [
    hello-flake-haskell
  ];
}

But when I try to enter the shell, I get an error:

❯ nix-shell
bash: /nix/store/ffbpbxg9cyj8nvj84d1si7k0g9ddyl0i-ghc-9.0.2-with-packages: Is a directory
bash: pop_var_context: head of shell_variables not a function context
bash: pop_var_context: head of shell_variables not a function context

I suspected that this might be an IFD problem, so I also tried nix-shell --allow-import-from-derivation, but I got the same error. How do I fix the problem?

In case it helps, here’s the flake.nix.

{
  description = "a flake for a simple Haskell program";

  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-22.11";
  };

  outputs = { self, nixpkgs }:
    let
      nameValuePair = name: value: { inherit name value; };
      genAttrs = names: f: builtins.listToAttrs (map (n: nameValuePair n (f n)) names);
      allSystems = [ "x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin" ];
      forAllSystems = f: genAttrs allSystems (system: f {
        pkgs = import nixpkgs { inherit system; };
      });
    in
    {
      packages = forAllSystems ({ pkgs }: {
        default = pkgs.haskellPackages.developPackage {
          name = "hello-flake-haskell";
          root = ./.;
        };
      });
    };
}
1 Like

I was able to solve the problem using the haskell-flake template instead of the zero-to-nix template.

The only thing I needed to modify was flake.nix, which looks like this:

{
  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
    flake-parts.url = "github:hercules-ci/flake-parts";
    haskell-flake.url = "github:srid/haskell-flake";
  };
  outputs = inputs@{ self, nixpkgs, flake-parts, ... }:
    flake-parts.lib.mkFlake { inherit inputs; } {
      systems = nixpkgs.lib.systems.flakeExposed;
      imports = [ inputs.haskell-flake.flakeModule ];

      perSystem = { self', pkgs, ... }: {

        # Typically, you just want a single project named "default". But
        # multiple projects are also possible, each using different GHC version.
        haskellProjects.default = {
          # If you have a .cabal file in the root, this option is determined
          # automatically. Otherwise, specify all your local packages here.
          # packages.hello-flake-haskell.root = ./.;

          # The base package set representing a specific GHC version.
          # By default, this is pkgs.haskellPackages.
          # You may also create your own. See https://haskell.flake.page/package-set
          # basePackages = pkgs.haskellPackages;

          # Dependency overrides go here. See https://haskell.flake.page/dependency
          # source-overrides = { };
          # overrides = self: super: { };

          # devShell = {
          #  # Enabled by default
          #  enable = true;
          #
          #  # Programs you want to make available in the shell.
          #  # Default programs can be disabled by setting to 'null'
          #  tools = hp: { fourmolu = hp.fourmolu; ghcid = null; };
          #
          #  hlsCheck.enable = true;
          # };
        };

        # haskell-flake doesn't set the default package, but you can do it here.
        packages.default = self'.packages.hello-flake-haskell;
      };
    };
}
1 Like

This has nothing to do with which flake you use. This is a bug in bash in current nixpkgs-unstable that triggers when the build crashes.

3 Likes