Skip to content

Commit

Permalink
Merge pull request #1320 from peterbecich/simplify-nix-workflow
Browse files Browse the repository at this point in the history
simplify Nix workflow
  • Loading branch information
peterbecich authored Jun 22, 2024
2 parents 1037f20 + abdde44 commit 6dbd9ed
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 49 deletions.
68 changes: 21 additions & 47 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,67 +13,41 @@ 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`.
The `flake.nix` is implemented with [`srid/haskell-flake`](https://github.com/srid/haskell-flake).

As with `nix build`, Hackage dependencies are not pulled from Hackage directly like usual. See caveat above.
Alternatively, open the [`nix develop`](https://nixos.org/manual/nix/stable/command-ref/new-cli/nix3-develop.html) shell:

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 develop

$ nix flake show
...
├───apps
...
│ │ ├───hackage-build: app
│ │ ├───hackage-import: app
│ │ ├───hackage-mirror: app
│ │ └───hackage-server: app
...
(in develop shell)

# if state directory does not already exist
$ cabal v2-run -- hackage-server init --static-dir=datafiles --state-dir=state

Run the `hackage-server` App:
$ 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

nix run .#hackage-server -- init --static-dir=datafiles
#### Populate the local package index

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`.
This copies packages from real Hackage Server to local Hackage Server.

`hackage-server` is the default App, so those commands can be shortened:
Add the default `admin` user to the `mirrorers` group here:
http://localhost:8080/packages/mirrorers/

nix run . -- init --static-dir=datafiles
Then

nix run . -- run --static-dir=datafiles --base-uri=http://127.0.0.1:8080
$ nix run .#mirror-hackage-server

##### Not working
#### Not working

Please note this App *cannot* be run [directly from GitHub](https://determinate.systems/posts/nix-run) like this:

Expand Down
39 changes: 37 additions & 2 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,43 @@
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}";
};
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 = {
Expand Down
9 changes: 9 additions & 0 deletions nix-default-servers.cfg
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 6dbd9ed

Please sign in to comment.