"nix flake show" on a Haskell flake

I have two repos with a basic “hello world” programs, one written in C, one written in Haskell. Both build and execute just fine. I can add the C version to my NixOS configuration, and it is installed and available as a command. I run into problems when I try to add the Haskell version to my NixOS configuration.

Running nix flake show on the C version, I see that the module name is hello-nix-flake.nixosModules.hello.

$ nix flake show github:mhwombat/hello-nix-flake
. . .
│   └───hello: NixOS module
. . .

I add that to my NixOS configuration, rebuild, and my custom hello is now available as a command. I feel victorious. I am the flake master! Mwah ha ha! Alas, pride goeth before a fall.

Next I try running nix flake show on the Haskell version. (The flake.nix file is based on templates#haskell-hello.)

$ nix flake show github:mhwombat/hello-haskell-flake
│   └───x86_64-linux
error: cannot build '/nix/store/kl94d5zyndm1vv8am6swjrn5dsm2msp7-cabal2nix-hello-haskell-flake.drv' during evaluation because the option 'allow-import-from-derivation' is disabled
(use '--show-trace' to show detailed location information)

I don’t know why the extra flag --allow-import-from-derivation is needed, but I take the hint:

$ nix flake show github:mhwombat/hello-haskell-flake --allow-import-from-derivation
│   └───x86_64-linux
│       └───hello-haskell-flake: derivation 'l6z88yi6vdyxp5hxy7xppi4j2xkdwwiw-source-0.0.0'
│   └───x86_64-linux: package 'l6z88yi6vdyxp5hxy7xppi4j2xkdwwiw-source-0.0.0'
│   └───x86_64-linux: development environment 'ghc-shell-for-l6z88yi6vdyxp5hxy7xppi4j2xkdwwiw-source-0.0.0-0'
├───overlay: Nixpkgs overlay
        └───hello-haskell-flake: package 'l6z88yi6vdyxp5hxy7xppi4j2xkdwwiw-source-0.0.0'

There’s no nixosModules section, so I can’t tell what to add to my NixOS configuration (also a flake) to install this on my system.

Note that I can build the flake at the command line, and run it just fine. For reference, here’s the flake:

  # inspired by: https://serokell.io/blog/practical-nix-flakes#packaging-existing-applications
  description = "A Hello World in Haskell with a dependency and a devShell";
  inputs.nixpkgs.url = "nixpkgs";
  outputs = { self, nixpkgs }:
      supportedSystems = [ "x86_64-linux" ];
      forAllSystems = f: nixpkgs.lib.genAttrs supportedSystems (system: f system);
      nixpkgsFor = forAllSystems (system: import nixpkgs {
        inherit system;
        overlays = [ self.overlay ];
      overlay = (final: prev: {
        hello-haskell-flake = final.haskellPackages.callCabal2nix "hello-haskell-flake" ./. {};
      packages = forAllSystems (system: {
         hello-haskell-flake = nixpkgsFor.${system}.hello-haskell-flake;
      defaultPackage = forAllSystems (system: self.packages.${system}.hello-haskell-flake);
      checks = self.packages;
      devShell = forAllSystems (system: let haskellPackages = nixpkgsFor.${system}.haskellPackages;
        in haskellPackages.shellFor {
          packages = p: [self.packages.${system}.hello-haskell-flake];
          withHoogle = true;
          buildInputs = with haskellPackages; [
        # Change the prompt to show that you are in a devShell
        shellHook = "export PS1='\\e[1;34mdev > \\e[0m'";
1 Like

The template should be adjusted then to not use callCabal2nix, as that uses IFD, which is forbidden in flakes by default.

About the module, if you need one, you can write it yourself, alternatively you can add ${inputName}.packages.${system}.hello to the environment.systemPackages of the system config where you want to use the program.

I created an issue because of IFD:

1 Like