ceedubs / unison-nix

Nix support for the Unison programming language
MIT License
56 stars 8 forks source link

How to use this declaratively in a flake? #43

Closed zetashift closed 1 year ago

zetashift commented 1 year ago

I'm attempting to use this in a flake.nix to get an easy nix develop workflow going, this command:

nix profile install github:ceedubs/unison-nix#ucm

gives me a working ucm, but I have no idea how to translate this into a flake.nix (my Nix is very weak sorry!). I tried looking around but couldn't find an example so far. I also tried making my own flake.nix from scratch but kept running against errors, the last one I got was:

 attribute 'unison-ucm' missing
zetashift commented 1 year ago

My current attempt at a flake:

{
  description = "Unison dev shell";

  inputs = {
    nixpkgs.url = github:nixos/nixpkgs/nixpkgs-unstable;
    flake-utils.url = github:numtide/flake-utils;
    unison.url = github:ceedubs/unison-nix;
    unison.flake = true;
  };

  outputs = { self, nixpkgs, unison, flake-utils, ... }:
    let
      forSystem = system:
        let
          pkgs = nixpkgs.legacyPackages.${system};
          ucm = unison.ucm;
        in
        {
          devShell = pkgs.mkShell {
            name = "unison-shell";
            buildInputs = [
              ucm
            ];
            shellHook = ''
            '';
          };
        };
    in
    flake-utils.lib.eachDefaultSystem forSystem;
}

which fails because it can't find unison.ucm so I'm defining something wrong :P.

ceedubs commented 1 year ago

@zetashift You were super close! I think that will work if you change the ucm definition to be something like ucm = unison.packages.${system}.ucm;.

An alternative approach that might be a bit handier if you think that you might depend on anything else from the Unison flake would be to run the Unison overlay on your pkgs. That would look something like this:

        let
          pkgs = import nixpkgs {
            inherit system;
            overlays = [ unison.overlay ];
          };
        in
        {
          devShell = pkgs.mkShell {
            name = "unison-shell";
            buildInputs = [
              pkgs.unison-ucm
            ];
            shellHook = ''
            '';
          };
        };

There is a lot of room for improvement in the docs for flakes (and Nix in general), but as a general tool for flakes, you can run nix flake show github:ceedubs/unison-nix which will show you the outputs of the flake.

ceedubs commented 1 year ago

I'm going to go ahead and close out this issue, but if the question comes up again then I'll take it as a sign that I should add some docs for this to the README.

zetashift commented 1 year ago

I forgot to say thank you, so thank you for the help, I got enough to get me started!

zetashift commented 1 year ago

On my work laptop getting the following error(on a m1 mac):

error: Package ‘unison-code-manager-1.0.M4e-alpha’ in
 /nix/store/flwlrj8zg135ib310lx0wdyg4b34ypfn-source/nix/ucm.nix:109 is not supported on ‘aarch64-darwin’, 
refusing to evaluate.
ceedubs commented 1 year ago

@zetashift yeah :(

Unfortunately ucm releases currently aren't built for aarch64. It will happen at some point, but I'm not sure when.

Until then, you can explicitly specify x86 as your system, and as long as you have Rosetta installed it should work find. here and here are some relevant examples from my dotfiles. Hope that helps!

zetashift commented 1 year ago

Ah damn, even the main website says it, I usually do all my dev on a Linux box so never had these problems haha. Eventual flake I settled with:

{
  description = "Unison dev shell";

  inputs = {
    nixpkgs.url = github:nixos/nixpkgs/nixpkgs-unstable;
    flake-utils.url = github:numtide/flake-utils;
    unison.url = github:ceedubs/unison-nix;
    unison.flake = true;
  };

  outputs = { self, nixpkgs, unison, flake-utils, ... }:
    let
      forSystem = system:
        let
          pkgs = import nixpkgs {
            system = "x86_64-darwin";
            overlays = [ unison.overlay ];
          };
        in
        {
          devShell = pkgs.mkShell {
            name = "unison-shell";
            buildInputs = [
              pkgs.unison-ucm
            ];
            shellHook = ''
            '';
          };
        };
    in
    flake-utils.lib.eachDefaultSystem forSystem;
}
ceedubs commented 1 year ago

@zetashift you probably don't want to set system to x86_64-darwin on a linux machine :)

If you always want x86_64-darwin then you set system = x86_64-darwin in your ~/.config/nix/nix.conf config. Or you can change your code to not bother with the eachDefaultSystem stuff.

zetashift commented 1 year ago

@zetashift you probably don't want to set system to x86_64-darwin on a linux machine :)

If you always want x86_64-darwin then you set system = x86_64-darwin in your ~/.config/nix/nix.conf config. Or you can change your code to not bother with the eachDefaultSystem stuff.

Yup, I noticed that a bit too late :P, thank you!