A haskell flake as a NixOS module?

I’ve created a very simple Haskell flake that doesn’t use IFD, so I expect to be able to include it in my NixOS configuration:

{
  description = "My NixOS Configuration";
  
  inputs = {
    nixpkgs.url = github:NixOS/nixpkgs/nixos-unstable;

    # My flakes
    hello-nix-flake.url = github:mhwombat/hello-nix-flake;
    hello-haskell-flake.url = github:mhwombat/hello-haskell-flake;
  };
  
  outputs = { self, nixpkgs, hello-nix-flake, hello-haskell-flake, ... }: {
    nixosConfigurations = {
      wombat11k = nixpkgs.lib.nixosSystem rec {
        system = "x86_64-linux";
        modules = [
            hello-nix-flake.nixosModules.hello
            hello-haskell-flake.nixosModules.${system}
            ./configuration.nix
          ];
      };
    };
  };
}

When I try to rebuild my NixOS, I get an error I don’t understand. I’m wasn’t sure if the syntax hello-haskell-flake.nixosModules.${system} was OK, but this error message seems to indicate that hello-haskell-flake isn’t visible?

$ sudo nixos-rebuild switch --upgrade
unpacking channels...
building the system configuration...
warning: Git tree '/home/amy/github/nixos-config' is dirty
error: The option `hello-haskell-flake' does not exist. Definition values:
       - In `/nix/store/f71alr07xb2swc37fiqjr5h416qcs6wr-source/flake.nix': <function, args: {pkgs}>

Here’s more info on the flake.

$ nix flake show github:mhwombat/hello-haskell-flake
github:mhwombat/hello-haskell-flake/e41c0c5d35a35d4350f68f5f2efc89a8a84ba047
├───defaultPackage
│   ├───aarch64-darwin: package 'hello-haskell-flake-0.1.0.0'
│   ├───aarch64-linux: package 'hello-haskell-flake-0.1.0.0'
│   ├───i686-linux: package 'hello-haskell-flake-0.1.0.0'
│   ├───x86_64-darwin: package 'hello-haskell-flake-0.1.0.0'
│   └───x86_64-linux: package 'hello-haskell-flake-0.1.0.0'
├───devShell
│   ├───aarch64-darwin: development environment 'nix-shell'
│   ├───aarch64-linux: development environment 'nix-shell'
│   ├───i686-linux: development environment 'nix-shell'
│   ├───x86_64-darwin: development environment 'nix-shell'
│   └───x86_64-linux: development environment 'nix-shell'
├───nixosModules
│   ├───aarch64-darwin: NixOS module
│   ├───aarch64-linux: NixOS module
│   ├───i686-linux: NixOS module
│   ├───x86_64-darwin: NixOS module
│   └───x86_64-linux: NixOS module
└───packages
    ├───aarch64-darwin
    │   └───hello-haskell-flake: package 'hello-haskell-flake-0.1.0.0'
    ├───aarch64-linux
    │   └───hello-haskell-flake: package 'hello-haskell-flake-0.1.0.0'
    ├───i686-linux
    │   └───hello-haskell-flake: package 'hello-haskell-flake-0.1.0.0'
    ├───x86_64-darwin
    │   └───hello-haskell-flake: package 'hello-haskell-flake-0.1.0.0'
    └───x86_64-linux
        └───hello-haskell-flake: package 'hello-haskell-flake-0.1.0.0'

Could you share the contents of your modules? I think the message is telling you the module is loaded correctly (or rather, there is no message telling you it isn’t), but there’s something fishy about your setting an option somewhere.

Certainly, the flake.nix for the module is:

{
  description = "My haskell application";

  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs";
    flake-utils.url = "github:numtide/flake-utils";
  };

  outputs = { self, nixpkgs, flake-utils }:
    flake-utils.lib.eachDefaultSystem (system:
      let
        pkgs = nixpkgs.legacyPackages.${system};

        haskellPackages = pkgs.haskellPackages;

        jailbreakUnbreak = pkg:
          pkgs.haskell.lib.doJailbreak (pkg.overrideAttrs (_: { meta = { }; }));

        packageName = "hello-haskell-flake";
        versionNum = "0.1.0.0";
      in {
        packages.${packageName} = pkgs.stdenv.mkDerivation rec {
          pname = "${packageName}";
          version = "${versionNum}";

          src = ./.;

          nativeBuildInputs = [
            pkgs.cabal-install
            pkgs.ghc
          ];

          unpackPhase =
            ''
              cp -r $src/* .
            '';

          configurePhase =
            ''
              mkdir -p cabal
              CABAL_DIR=$PWD/cabal cabal user-config init
              sed --in-place '/^repository /d; /^ *url:/d; /^ *--/d' cabal/config
            '';

          buildPhase =
            ''
              CABAL_DIR=$PWD/cabal cabal build
            '';

          installPhase =
            ''
              CABAL_DIR=$PWD/cabal cabal install
              mkdir -p $out/bin
              cp cabal/bin/${packageName} $out/bin/
            '';
        };

        defaultPackage = self.packages.${system}.${packageName};

        # This stuff will be available when you run nix develop, but not nix build.
        devShell = pkgs.mkShell {
          buildInputs = with haskellPackages; [
            haskell-language-server
            ghcid
            cabal-install
          ];
          inputsFrom = builtins.attrValues self.packages.${system};
        };

        # A NixOS module, if applicable (e.g. if the package provides a system service).
        nixosModules.${packageName}  =
          { pkgs, ... }:
          {
            nixpkgs.overlays = [ self.overlay ];
            environment.systemPackages = [ pkgs.${packageName}  ];
            #systemd.services = { ... };
          };
      });
}

I’m guessing you don’t need to see the cabal file or source code, but just in case, they’re at GitHub - mhwombat/hello-haskell-flake: A simple Haskell flake for testing.

I included the flake.nix from my NixOS configuration in my original post, and I’m guessing the rest of my config isn’t an issue because it is invoked after the bit that triggers the error message, but just in case, it’s all available at: GitHub - mhwombat/nixos-config: My NixOS configuration.

You include hello-haskell-flake.nixosModules.${system} in the modules list but that should be hello-haskell-flake.nixosModules.${system}.hello-haskell-flake.

1 Like

Even better fix would be to not have the nixosModules have the system indirection, by moving them out of the FU call.

Or perhaps remove FU all together?

1 Like