From bb3b5adcf391a7a48ef145109cc3b2ede7b7a040 Mon Sep 17 00:00:00 2001 From: Peter Becich Date: Sat, 22 Jun 2024 10:39:30 -0700 Subject: [PATCH 1/2] simplify Nix workflow https://github.com/srid/haskell-flake/discussions/330 --- README.md | 63 ++++++++++++------------------------------------------- flake.nix | 23 ++++++++++++++++++-- 2 files changed, 34 insertions(+), 52 deletions(-) diff --git a/README.md b/README.md index 51cd6f01..32641317 100644 --- a/README.md +++ b/README.md @@ -13,65 +13,28 @@ 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: - -#### [`nix develop`](https://nixos.org/manual/nix/stable/command-ref/new-cli/nix3-develop.html) - - nix develop - - (in develop shell) - $ cabal v2-run -- hackage-server init --static-dir=datafiles - - $ cabal v2-run -- hackage-server run --static-dir=datafiles --base-uri=http://127.0.0.1:8080 + '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. -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 - ... +The `flake.nix` is implemented with [`srid/haskell-flake`](https://github.com/srid/haskell-flake). -Run the `hackage-server` App: +Alternatively, open the [`nix develop`](https://nixos.org/manual/nix/stable/command-ref/new-cli/nix3-develop.html) shell: - nix run .#hackage-server -- init --static-dir=datafiles + $ nix develop - nix run .#hackage-server -- run --static-dir=datafiles --base-uri=http://127.0.0.1:8080 + (in develop shell) -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: + # if state directory does not already exist + $ cabal v2-run -- hackage-server init --static-dir=datafiles --state-dir=state - nix run . -- init --static-dir=datafiles - - nix run . -- run --static-dir=datafiles --base-uri=http://127.0.0.1:8080 + $ cabal v2-run -- hackage-server run --static-dir=datafiles --state-dir=state --base-uri=http://127.0.0.1:8080 + hackage-server: Ready! Point your browser at http://127.0.0.1:8080 ##### Not working 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 = { From abdde4433152151f042c61565ee6acb48dff2a75 Mon Sep 17 00:00:00 2001 From: Peter Becich Date: Sat, 22 Jun 2024 11:00:05 -0700 Subject: [PATCH 2/2] simple package mirroring workflow --- README.md | 13 ++++++++++++- flake.nix | 16 ++++++++++++++++ nix-default-servers.cfg | 9 +++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 nix-default-servers.cfg diff --git a/README.md b/README.md index 32641317..3a12efbf 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,18 @@ Alternatively, open the [`nix develop`](https://nixos.org/manual/nix/stable/comm $ cabal v2-run -- hackage-server run --static-dir=datafiles --state-dir=state --base-uri=http://127.0.0.1:8080 hackage-server: Ready! Point your browser at http://127.0.0.1:8080 -##### Not working +#### Populate the local package index + +This copies packages from real Hackage Server to local Hackage Server. + +Add the default `admin` user to the `mirrorers` group here: +http://localhost:8080/packages/mirrorers/ + +Then + + $ nix run .#mirror-hackage-server + +#### 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 3d61c676..e37e52c4 100644 --- a/flake.nix +++ b/flake.nix @@ -36,6 +36,22 @@ }; in "${lib.getExe run-hackage-server}"; }; + apps.mirror-hackage-server = { + type = "app"; + program = + let + mirror-hackage-server = pkgs.writeShellApplication { + name = "mirror-hackage-server"; + runtimeInputs = [ config.packages.default ]; + text = '' + echo 'Copying packages from real Hackage Server into local Hackage Server.' + echo 'This assumes the local Hackage Server uses default credentials;' + echo 'otherwise, override in nix-default-servers.cfg' + hackage-mirror nix-default-servers.cfg + ''; + }; + in "${lib.getExe mirror-hackage-server}"; + }; packages.default = config.packages.hackage-server; haskellProjects.default = { settings = { diff --git a/nix-default-servers.cfg b/nix-default-servers.cfg new file mode 100644 index 00000000..b61d4382 --- /dev/null +++ b/nix-default-servers.cfg @@ -0,0 +1,9 @@ +source "hackage" + uri: http://hackage.haskell.org + type: secure + +target "mirror" + uri: http://admin:admin@localhost:8080 + type: hackage2 + + post-mirror-hook: "shell command to execute"