How can I install a Haskell library to be accessible via GHCi with Nixos?

前端 未结 2 784
渐次进展
渐次进展 2021-02-09 02:11

I\'ve managed to install ghc with nix-env -i ghc.

I\'d like to install a Haskell library now, how should this be done? For example the turtle (

相关标签:
2条回答
  • 2021-02-09 02:36

    As an alternative Robert's answer, one can use a nix-shell environment by creating a shell.nix file with contents of:

    { pkgs ? import <nixpkgs> {} }:
    let myGhc = pkgs.haskellPackages.ghcWithPackages (hpkgs: with hpkgs; [
          turtle
        ]);
    in
    pkgs.mkShell {
      buildInputs = [ myGhc ];
    }
    

    And entering this environment with nix-shell.

    Alternatively this single command nix-shell -p "haskellPackages.ghcWithPackages (pkgs: [pkgs.turtle])".

    0 讨论(0)
  • 2021-02-09 02:57

    This works differently on NixOS because of purity. NixOS' GHC will only look at its own immutable installation directory and the packages that have been installed by the user with cabal install.

    What you can do is install into your user profile a GHC wrapper that supplies a nice set of packages when you run ghci.

    Create a file my-ghc.nix:

    (import <nixpkgs> {}).haskellPackages.ghcWithPackages (hpkgs: with hpkgs; [
        lens
        aeson
        turtle
    ])
    

    Uninstall your previous attempt, to avoid name collisions:

    nix-env -e ghc turtle
    

    Install the wrapped GHC:

    nix-env -if my-ghc.nix
    

    You may edit the file in the future and re-run that command.

    When I am working on a project, I prefer to use cabal2nix and nix-shell. (For an introduction, see Oliver Charles' blog post)

    0 讨论(0)
提交回复
热议问题