Skip to content

Commit 1162ccb

Browse files
committed
Revert "Update getting-started.md"
This reverts commit eb16a35.
1 parent eb16a35 commit 1162ccb

File tree

1 file changed

+82
-8
lines changed

1 file changed

+82
-8
lines changed

docs/tutorials/getting-started.md

Lines changed: 82 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ This can be tricky to get setup properly. If you're still having trouble getting
4646

4747
## Create a project using Flakes
4848

49-
This section assumes you choose to uses the experimental flakes features, and so that you have added `experimental-features = [ "nix-command" "flakes" ];` in your Nix configuration. You can look at [the Wiki](https://nixos.wiki/wiki/Flakes) for more instructions.
49+
This section assumes you choose to uses the experimental flakes features, and so that you have added `experimental-features = [ "nix-command" "flakes" ];` in your Nix configuration. You can look at [the Wiki](https://nixos.wiki/wiki/Flakes) for more instructions. If you don't want to set up a Flakes project, [skip to the Niv section](#create-a-project-using-niv).
5050

5151
The following `nix flake init` command creates a template `hello` package containing a `flake.nix` and `nix/hix.nix` file. The project can be used with
5252
regular `nix` tools. This template is defined in the [NixOS/templates repository](https://github.com/NixOS/templates/tree/master/haskell.nix).
@@ -72,14 +72,59 @@ To build and run a component:
7272
nix run .#hello:exe:hello
7373
```
7474

75+
## Create a project using Niv
76+
77+
[Niv](https://github.com/nmattia/niv) is a command line tool for keeping track of Nix project dependencies. You don't need it if you [set up your project with Flakes](#create-a-project-using-flakes), and so you can skip this section.
78+
79+
This guide assumes that the `sources.haskellNix` will be set to point a pinned copy of the `haskell.nix` github repo. One easy way to do this is to use Niv. If you prefer not to use Niv another option is described in the in the following "[Using `haskell.nix` without Niv](#using-1-without-Niv)" section of this document.
80+
81+
After installing niv you can initialize niv and pin the latest `haskell.nix` commit by running the following in the root directory of the project:
82+
```shell
83+
niv init
84+
niv add input-output-hk/haskell.nix -n haskellNix
85+
```
86+
87+
Then when you want to update to the latest version of `haskellNix` use:
88+
```shell
89+
niv update haskellNix
90+
```
91+
92+
### Using `haskell.nix` without Niv
93+
94+
If you would prefer not to use niv you can replace `sources = import ./nix/sources.nix {};` in the examples with:
95+
```nix
96+
let sources = {
97+
haskellNix = builtins.fetchTarball "https://github.com/input-output-hk/haskell.nix/archive/master.tar.gz";
98+
};
99+
```
100+
101+
The `fetchTarball` call above will always get the latest version, and is similar to an auto-updating Nix channel.
102+
103+
However, in your own project, you may wish to pin `haskell.nix` (as you would pin Nixpkgs). This will make your builds reproducible, more predictable, and faster (because the fixed version is cached).
104+
105+
Straightforward way of doing this is to change the branch name to a revision.
106+
```nix
107+
let sources = {
108+
haskellNix = builtins.fetchTarball "https://github.com/input-output-hk/haskell.nix/archive/f1a94a4c82a2ab999a67c3b84269da78d89f0075.tar.gz";
109+
};
110+
```
111+
112+
There are other possible schemes for pinning. See [Bumping Hackage and Stackage snapshots](./hackage-stackage.md) and [Nix tutorial on reproducibility using pinning](https://nix.dev/tutorials/towards-reproducibility-pinning-nixpkgs.html).
113+
75114
## Scaffolding
76115

77116
The following code could be capy-pasted and will work with `stack.yaml` and `cabal.project` based projects.
78117

79-
Edit your `flake.nix` as:```nix
118+
For a Flakes-based project, use a `flake.nix`:
119+
```nix
80120
{{#include getting-started-flakes/flake.nix}}
81121
```
82122

123+
For a legacy (Nix) Nix project rather, use a `default.nix`:
124+
```nix
125+
{{#include getting-started/default.nix}}
126+
```
127+
83128
> **Note:** Git dependencies
84129
>
85130
> If you have git dependencies in your project, you'll need to [calculate sha256 hashes for them](./source-repository-hashes.md).
@@ -91,14 +136,32 @@ Top-level attributes are Haskell packages (incl. dependencies) part of your proj
91136
This section will show side by side the commands using Flakes experimental `new-command` API and legacy Nix commands.
92137

93138
To build the library component of a package in the project run:
94-
```shell
95-
nix build .#your-package-name:lib:your-package-name
96-
```
139+
140+
* With flakes experimental commands:
141+
```shell
142+
nix build .#your-package-name:lib:your-package-name
143+
```
144+
* With Nix legacy commands:
145+
```shell
146+
nix-build -A your-package-name.components.library
147+
```
97148

98149
There are also other components such as `exes`, `tests`, `benchmarks` and `all`.
99150
To build an executable:
100-
```shell
101-
nix build .#your-package-name:exe:your-exe-name
151+
152+
* With flakes experimental commands:
153+
```shell
154+
nix build .#your-package-name:exe:your-exe-name
155+
```
156+
* With Nix legacy commands:
157+
```shell
158+
nix-build -A your-package-name.components.exes.your-exe-name
159+
```
160+
161+
Cross compilation is builtins into Flakes, but to cross-compile with legacy Nix commands, use the `projectCross` attribute:
162+
```
163+
nix-build -A projectCross.ghcjs.hsPkgs.your-package-name.components.exes.your-exe-name
164+
nix-build -A projectCross.mingwW64.hsPkgs.your-package-name.components.exes.your-exe-name
102165
```
103166

104167
Flakes provide a `devShell` attribute that allow you to spawn a developer shell, here with `cabal`, `hlint` and `haskell-language-server`:
@@ -108,6 +171,17 @@ cabal repl your-package-name:lib:your-package-name
108171
cabal build your-package-name
109172
```
110173

174+
Nix legacy commands needs you to add first a `shell.nix`:
175+
```nix
176+
{{#include getting-started/shell.nix}}
177+
```
178+
Then you can run:
179+
```shell
180+
nix-shell
181+
cabal new-repl your-package-name:library:your-package-name
182+
cabal new-build your-package-name
183+
```
184+
111185
To open a shell for use with `stack` see [the following issue](https://github.com/input-output-hk/haskell.nix/issues/689#issuecomment-643832619).
112186

113187
## Getting started with Hix
@@ -179,4 +253,4 @@ hix-build -A hsPkgs.hello.components.exes.hello
179253

180254
Read through [project](../reference/library.md#project) function reference to see how the API works.
181255

182-
There are a number of things to explore further in the tutorials section.
256+
There are a number of things to explore further in the tutorials section.

0 commit comments

Comments
 (0)