From 6b9a4959881164699891fb6235f58cb49adabc38 Mon Sep 17 00:00:00 2001 From: Peter Becich Date: Sat, 22 Jun 2024 10:39:30 -0700 Subject: [PATCH] simplify Nix workflow https://github.com/srid/haskell-flake/discussions/330 --- README.md | 57 +++++++++---------------------------------------------- flake.nix | 23 ++++++++++++++++++++-- 2 files changed, 30 insertions(+), 50 deletions(-) diff --git a/README.md b/README.md index 51cd6f01..d4570374 100644 --- a/README.md +++ b/README.md @@ -13,13 +13,18 @@ You can use the Nix package manager to provide these dependencies, or install th ### Using the [Nix package manager](https://nixos.org/) and provided [Nix Flake](https://nixos.org/manual/nix/stable/command-ref/new-cli/nix3-flake.html) -If you have the Nix package manager installed, you can build and run `hackage-server` without manually installing any dependencies. +If you have the Nix package manager installed, you can build and run `hackage-server` without manually installing any dependencies: -This uses `flake.nix`, implemented with [`srid/haskell-flake`](https://github.com/srid/haskell-flake). + $ nix run -There are at least three ways to use this `flake.nix`. Clone this repository, enter the repository directory, then choose one of these options: + 'state' state-dir already exists + hackage-server: Ready! Point your browser at http://127.0.0.1:8080 + +If the required `state` directory does not already exist, `nix run` will create and initialize it. + +The `flake.nix` is implemented with [`srid/haskell-flake`](https://github.com/srid/haskell-flake). -#### [`nix develop`](https://nixos.org/manual/nix/stable/command-ref/new-cli/nix3-develop.html) +Alternatively, open the [`nix develop`](https://nixos.org/manual/nix/stable/command-ref/new-cli/nix3-develop.html) shell: nix develop @@ -29,50 +34,6 @@ There are at least three ways to use this `flake.nix`. Clone this repository, en $ cabal v2-run -- hackage-server run --static-dir=datafiles --base-uri=http://127.0.0.1:8080 hackage-server: Ready! Point your browser at http://127.0.0.1:8080 -Note the `init` command will create a new folder `state` in your working directory. - -If you have [direnv](https://direnv.net/), `direnv allow` will load this `nix develop` shell automatically. - -#### [`nix build`](https://nixos.org/manual/nix/stable/command-ref/nix-build.html) - - nix build - -This will produce a `hackage-server` executable in `result/`. - -For this executable, Hackage dependencies are not pulled from Hackage directly like usual. Hackage dependencies are provided by the [Nixpkgs](https://search.nixos.org/packages) [`haskell-updates`](https://github.com/NixOS/nixpkgs/tree/haskell-updates) branch, and a few [overrides in `flake.nix`](https://zero-to-flakes.com/haskell-flake/dependency#using-hackage-versions). - -#### [`nix run`](https://nixos.org/manual/nix/stable/command-ref/new-cli/nix3-run) - -`nix run` is more convenient to use than `nix build`. - -As with `nix build`, Hackage dependencies are not pulled from Hackage directly like usual. See caveat above. - -List the available [Flake Apps](https://nixos.org/manual/nix/stable/command-ref/new-cli/nix3-run#apps) with [`nix flake show`](https://nixos.org/manual/nix/stable/command-ref/new-cli/nix3-flake-show.html): - - $ nix flake show - ... - ├───apps - ... - │ │ ├───hackage-build: app - │ │ ├───hackage-import: app - │ │ ├───hackage-mirror: app - │ │ └───hackage-server: app - ... - -Run the `hackage-server` App: - - nix run .#hackage-server -- init --static-dir=datafiles - - nix run .#hackage-server -- run --static-dir=datafiles --base-uri=http://127.0.0.1:8080 - -The `.` refers to the `flake.nix` in your working directory. `#hackage-server` refers to the App specified in that `flake.nix`. - -`hackage-server` is the default App, so those commands can be shortened: - - nix run . -- init --static-dir=datafiles - - nix run . -- run --static-dir=datafiles --base-uri=http://127.0.0.1:8080 - ##### Not working Please note this App *cannot* be run [directly from GitHub](https://determinate.systems/posts/nix-run) like this: diff --git a/flake.nix b/flake.nix index c71960ba..3d61c676 100644 --- a/flake.nix +++ b/flake.nix @@ -15,8 +15,27 @@ inputs.flake-root.flakeModule ]; perSystem = { self', system, lib, config, pkgs, ... }: { - # The "main" project. You can have multiple projects, but this template - # has only one. + apps.default = { + type = "app"; + program = + let + run-hackage-server = pkgs.writeShellApplication { + name = "run-hackage-server"; + runtimeInputs = [ config.packages.default ]; + text = '' + if [ ! -d "state" ]; then + hackage-server init --static-dir=datafiles --state-dir=state + else + echo "'state' state-dir already exists" + fi + hackage-server run \ + --static-dir=datafiles \ + --state-dir=state \ + --base-uri=http://127.0.0.1:8080 + ''; + }; + in "${lib.getExe run-hackage-server}"; + }; packages.default = config.packages.hackage-server; haskellProjects.default = { settings = {