Skip to content

dwayne/elm2nix

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

elm2nix

A rewrite of cachix/elm2nix with a few changes and improvements.

These are some of the notable differences:

  1. You can input one or more elm.json files to create the lock file.
  2. It uses a JSON formatted lock file that has support for multiple versions of the same package.
  3. You don't have to manually generate a registry.dat file since it's automatically handled for you.
  4. You can display any registry.dat file as JSON using elm2nix registry view.
  5. There is a buildElmApplication build helper that allows you to build an Elm application in a variety of ways by setting options. For e.g.
    • Turn on the time-travelling debugger
    • Fail the build if your Elm source code is improperly formatted
    • Fail the build if your Elm tests fail
    • Fail the build if elm-review fails
    • Turn on optimizations
    • Use elm-optimize-level-2 instead of elm make --optimize
    • Enable minification with UglifyJS or Terser
    • Enable compression with gzip and brotli
    • Show a report about the changes in your file size due to minification and compression
    • Enable content hashing for cache busting purposes
    • Or completely customize portions of the build to your liking if you know how stdenv.mkDerivation works

Usage

Overview

In the folder containing your Elm application's elm.json you use:

  • elm2nix lock to generate an elm.lock lock file

The lock file is used by a build helper, called buildElmApplication, to build your Elm application.

Details

  1. Add dwayne/elm2nix as an input to your flake.
inputs.elm2nix.url = "github:dwayne/elm2nix";
  1. Add it's default package to your development shell.
outputs = { self, nixpkgs, flake-utils, elm2nix }:
    flake-utils.lib.eachDefaultSystem(system:
      let
        pkgs = nixpkgs.legacyPackages.${system};
      in
      {
        devShells.default = pkgs.mkShell {
          packages = [
            elm2nix.packages.${system}.default
            ...
          ];

          ...
        };

        ...
      }
    );
  1. Use the elm2nix program to generate the elm.lock file from your Elm application's elm.json.
nix develop
elm2nix lock

See elm2nix lock --help for more details.

  1. Use buildElmApplication to build your Elm application.
outputs = { self, nixpkgs, flake-utils, elm2nix }:
    flake-utils.lib.eachDefaultSystem(system:
      let
        pkgs = nixpkgs.legacyPackages.${system};
        inherit (elm2nix.lib.elm2nix pkgs) buildElmApplication;

        myApp = buildElmApplication {
          name = "my-app";
          src = ./.;
          elmLock = ./elm.lock;
        };
      in
      {
        packages.myApp = myApp;

        ...
      }
    );
  1. Build your Elm application.
nix build .#myApp

This will generate a result/ directory containing your compiled application in elm.js.

  1. That's it.

You can view a full working example in the example/ directory.

About

I was looking for a practical and interesting project to work on that might be beneficial to either the Elm or Haskell community, while at the same time, would extend my skills within functional programming. When I researched cachix/elm2nix it seemed to fit the bill because it combined 3 technologies I love, Elm, Haskell, and Nix, to produce a useful tool for Elm developers. At the time, I knew how to use each of the technologies to varying degrees but I didn't have a deep understanding of how cachix/elm2nix worked and why it needed to work that way. As a result, it seemed like a great project to satisfy my goals. Thankfully it turned out well. I learned so much about Elm, Haskell, and Nix that I didn't know before and I was able to find little ways to positively improve upon the project.

References

A grossly incomplete list of resources that helped me while working on the project.

About

A rewrite of cachix/elm2nix with a few changes and improvements.

Topics

Resources

License

Stars

Watchers

Forks