Skip to content

Commit bb3b5ad

Browse files
committed
simplify Nix workflow
srid/haskell-flake#330
1 parent 1037f20 commit bb3b5ad

File tree

2 files changed

+34
-52
lines changed

2 files changed

+34
-52
lines changed

README.md

Lines changed: 13 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -13,65 +13,28 @@ 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`.
47-
48-
As with `nix build`, Hackage dependencies are not pulled from Hackage directly like usual. See caveat above.
49-
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):
51-
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-
...
25+
The `flake.nix` is implemented with [`srid/haskell-flake`](https://github.com/srid/haskell-flake).
6126

62-
Run the `hackage-server` App:
27+
Alternatively, open the [`nix develop`](https://nixos.org/manual/nix/stable/command-ref/new-cli/nix3-develop.html) shell:
6328

64-
nix run .#hackage-server -- init --static-dir=datafiles
29+
$ nix develop
6530

66-
nix run .#hackage-server -- run --static-dir=datafiles --base-uri=http://127.0.0.1:8080
31+
(in develop shell)
6732

68-
The `.` refers to the `flake.nix` in your working directory. `#hackage-server` refers to the App specified in that `flake.nix`.
69-
70-
`hackage-server` is the default App, so those commands can be shortened:
33+
# if state directory does not already exist
34+
$ cabal v2-run -- hackage-server init --static-dir=datafiles --state-dir=state
7135

72-
nix run . -- init --static-dir=datafiles
73-
74-
nix run . -- run --static-dir=datafiles --base-uri=http://127.0.0.1:8080
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
7538

7639
##### Not working
7740

flake.nix

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,27 @@
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+
};
2039
packages.default = config.packages.hackage-server;
2140
haskellProjects.default = {
2241
settings = {

0 commit comments

Comments
 (0)