Skip to content

Commit 6dbd9ed

Browse files
authored
Merge pull request #1320 from peterbecich/simplify-nix-workflow
simplify Nix workflow
2 parents 1037f20 + abdde44 commit 6dbd9ed

File tree

3 files changed

+67
-49
lines changed

3 files changed

+67
-49
lines changed

README.md

Lines changed: 21 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -13,67 +13,41 @@ You can use the Nix package manager to provide these dependencies, or install th
1313

1414
### 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)
1515

16-
If you have the Nix package manager installed, you can build and run `hackage-server` without manually installing any dependencies.
16+
If you have the Nix package manager installed, you can build and run `hackage-server` without manually installing any dependencies:
1717

18-
This uses `flake.nix`, implemented with [`srid/haskell-flake`](https://github.com/srid/haskell-flake).
18+
$ nix run
1919

20-
There are at least three ways to use this `flake.nix`. Clone this repository, enter the repository directory, then choose one of these options:
21-
22-
#### [`nix develop`](https://nixos.org/manual/nix/stable/command-ref/new-cli/nix3-develop.html)
23-
24-
nix develop
25-
26-
(in develop shell)
27-
$ cabal v2-run -- hackage-server init --static-dir=datafiles
28-
29-
$ cabal v2-run -- hackage-server run --static-dir=datafiles --base-uri=http://127.0.0.1:8080
20+
'state' state-dir already exists
3021
hackage-server: Ready! Point your browser at http://127.0.0.1:8080
22+
23+
If the required `state` directory does not already exist, `nix run` will create and initialize it.
3124

32-
Note the `init` command will create a new folder `state` in your working directory.
33-
34-
If you have [direnv](https://direnv.net/), `direnv allow` will load this `nix develop` shell automatically.
35-
36-
#### [`nix build`](https://nixos.org/manual/nix/stable/command-ref/nix-build.html)
37-
38-
nix build
39-
40-
This will produce a `hackage-server` executable in `result/`.
41-
42-
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).
43-
44-
#### [`nix run`](https://nixos.org/manual/nix/stable/command-ref/new-cli/nix3-run)
45-
46-
`nix run` is more convenient to use than `nix build`.
25+
The `flake.nix` is implemented with [`srid/haskell-flake`](https://github.com/srid/haskell-flake).
4726

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

50-
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):
29+
$ nix develop
5130

52-
$ nix flake show
53-
...
54-
├───apps
55-
...
56-
│ │ ├───hackage-build: app
57-
│ │ ├───hackage-import: app
58-
│ │ ├───hackage-mirror: app
59-
│ │ └───hackage-server: app
60-
...
31+
(in develop shell)
32+
33+
# if state directory does not already exist
34+
$ cabal v2-run -- hackage-server init --static-dir=datafiles --state-dir=state
6135

62-
Run the `hackage-server` App:
36+
$ cabal v2-run -- hackage-server run --static-dir=datafiles --state-dir=state --base-uri=http://127.0.0.1:8080
37+
hackage-server: Ready! Point your browser at http://127.0.0.1:8080
6338

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

66-
nix run .#hackage-server -- run --static-dir=datafiles --base-uri=http://127.0.0.1:8080
67-
68-
The `.` refers to the `flake.nix` in your working directory. `#hackage-server` refers to the App specified in that `flake.nix`.
41+
This copies packages from real Hackage Server to local Hackage Server.
6942

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

72-
nix run . -- init --static-dir=datafiles
46+
Then
7347

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

76-
##### Not working
50+
#### Not working
7751

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

flake.nix

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,43 @@
1515
inputs.flake-root.flakeModule
1616
];
1717
perSystem = { self', system, lib, config, pkgs, ... }: {
18-
# The "main" project. You can have multiple projects, but this template
19-
# has only one.
18+
apps.default = {
19+
type = "app";
20+
program =
21+
let
22+
run-hackage-server = pkgs.writeShellApplication {
23+
name = "run-hackage-server";
24+
runtimeInputs = [ config.packages.default ];
25+
text = ''
26+
if [ ! -d "state" ]; then
27+
hackage-server init --static-dir=datafiles --state-dir=state
28+
else
29+
echo "'state' state-dir already exists"
30+
fi
31+
hackage-server run \
32+
--static-dir=datafiles \
33+
--state-dir=state \
34+
--base-uri=http://127.0.0.1:8080
35+
'';
36+
};
37+
in "${lib.getExe run-hackage-server}";
38+
};
39+
apps.mirror-hackage-server = {
40+
type = "app";
41+
program =
42+
let
43+
mirror-hackage-server = pkgs.writeShellApplication {
44+
name = "mirror-hackage-server";
45+
runtimeInputs = [ config.packages.default ];
46+
text = ''
47+
echo 'Copying packages from real Hackage Server into local Hackage Server.'
48+
echo 'This assumes the local Hackage Server uses default credentials;'
49+
echo 'otherwise, override in nix-default-servers.cfg'
50+
hackage-mirror nix-default-servers.cfg
51+
'';
52+
};
53+
in "${lib.getExe mirror-hackage-server}";
54+
};
2055
packages.default = config.packages.hackage-server;
2156
haskellProjects.default = {
2257
settings = {

nix-default-servers.cfg

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
source "hackage"
2+
uri: http://hackage.haskell.org
3+
type: secure
4+
5+
target "mirror"
6+
uri: http://admin:admin@localhost:8080
7+
type: hackage2
8+
9+
post-mirror-hook: "shell command to execute"

0 commit comments

Comments
 (0)