Skip to content

Commit a1fd791

Browse files
committed
Rework Nix example project & add Nix template
Changes the Nix infrastructure inside of simple-nix to use Nix flakes. Adds some more explanation how to include dependencies using Nix and includes a Nix template. All of this was done to make it more user friendly (...for Nix users) to get started with Clash without having to have very deep knowledge of Nix or how Cabal works to make Nix and Cabal work together happily. People who have Nix installed can now use immediately start using Clash with the simple-nix directory by running three commands: ``` mkdir my-project cd my-project nix flake init -t github:clash-lang/clash-starters ``` After this nix command is executed, it will display a 'welcome' message which gives a plain overview of what the Nix related commands do and instructs the user to read the README.md for more information. The original PR is: clash-lang/clash-starters#6 Which I have closed after realizing that repository is essentially just a mirror
1 parent ce91289 commit a1fd791

File tree

7 files changed

+628
-86
lines changed

7 files changed

+628
-86
lines changed

flake.nix

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
description = "A flake for clash-starter templates. For an actual flake containing the project, check the `simple-nix` subdirectory.";
3+
4+
outputs = { self }: {
5+
defaultTemplate = {
6+
path = ./projects/simple-nix;
7+
description = "A template containing a simple Clash project with a Nix flake for development";
8+
welcomeText = ''
9+
# Nix development
10+
To use Nix to develop your Clash project, simply run `nix develop` in your current directory.
11+
This will put you into an environment with all the tools you will need to build the starter
12+
project.
13+
14+
To compile the project and generate HDL, simply run `nix run` and the output Verilog will be
15+
under the `verilog` directory. If you changed the HDL language to VHDL, it will be under the
16+
`vhdl` directory.
17+
18+
You can also run `nix build` to build the project using Nix. Although this isn't very
19+
useful in the starter project, it is very useful when developing libraries!
20+
21+
# Cabal & Nix clashes
22+
Cabal and Nix work happily together, most of the time. One important thing of note is that
23+
you should **not** define sources using `cabal.project` files. This will overwrite Nix's
24+
package source and *will* cause problems. To add dependencies, add them via `flake.nix`.
25+
26+
# TLDR
27+
1. run `nix develop` to immediately jump into development
28+
2. run `nix run` to build the package and generate HDL with Nix
29+
3. run `nix build` to build the package as a library with Nix
30+
31+
Read the README.md for more information!
32+
Happy hacking!
33+
'';
34+
};
35+
};
36+
}

projects/simple-nix/default.nix

Lines changed: 0 additions & 23 deletions
This file was deleted.

projects/simple-nix/flake.lock

Lines changed: 215 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

projects/simple-nix/flake.nix

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
{
2+
description = "A flake for the Clash starter project";
3+
inputs = {
4+
nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
5+
clash-compiler.url = "github:clash-lang/clash-compiler?rev=v1.8.4";
6+
};
7+
outputs = { self, nixpkgs, flake-utils, clash-compiler }:
8+
flake-utils.lib.eachDefaultSystem (system:
9+
let
10+
# A list of regular packages you want to use with your project
11+
pkgs = (import nixpkgs {
12+
inherit system;
13+
});
14+
15+
# What version of GHC you want to use
16+
# The version must be in the list of supported versions of the clash-compiler!
17+
ghc-version = "ghc9101";
18+
19+
# A list of HASKELL packages clash-compiler uses, we use this set packages to prevent version
20+
# conflicts with the regular nixpkgs when using Clash
21+
clash-pkgs = ((import clash-compiler.inputs.nixpkgs {
22+
inherit system;
23+
}).extend clash-compiler.overlays.${ghc-version})."clashPackages-${ghc-version}";
24+
25+
package-overlay = final: prev: {
26+
# Here you define the project you want to build
27+
simple-nix = prev.developPackage {
28+
root = ./.;
29+
overrides = _: _: final;
30+
};
31+
};
32+
33+
# Create a set of Haskell packages including ours!
34+
hs-pkgs = clash-pkgs.extend package-overlay;
35+
36+
# Options for `nix run`
37+
# Select the toplevel module
38+
top-module = "Example.Project";
39+
# Output VHDL or Verilog
40+
hdl = "verilog";
41+
in
42+
{
43+
# Develop the project using `nix develop`
44+
devShells.default = hs-pkgs.shellFor {
45+
# Under `packages` we list out all Haskell packages we want to develop *for*
46+
# This will then grab all the dependencies for those packages
47+
# Note that this does *not* build the package itself! Only the dependencies of the package
48+
packages = p: [
49+
p.simple-nix
50+
];
51+
52+
nativeBuildInputs = [
53+
# Standard
54+
hs-pkgs.cabal-install
55+
hs-pkgs.cabal-plan
56+
hs-pkgs.fourmolu
57+
# HLS is commented out as it takes long to compile, but if you want it, here it is:
58+
# hs-pkgs.haskell-language-server
59+
60+
# Regular dependencies you may want to use in your project
61+
pkgs.hello
62+
];
63+
};
64+
65+
# Build the project with Clash and generate HDL
66+
# This can be ran using `nix run` and will output Verilog files under the `verilog` subdirectory
67+
# The top-module and hdl are variables defined above in the let statement
68+
apps.default = {
69+
type = "app";
70+
program = (pkgs.writeShellScript "compile" ''
71+
cabal build
72+
cabal run clash ${top-module} -- --${hdl}
73+
'').outPath;
74+
};
75+
76+
# Build the project using `nix build`
77+
packages.default = hs-pkgs.simple-nix;
78+
}
79+
);
80+
}

projects/simple-nix/release.nix

Lines changed: 0 additions & 4 deletions
This file was deleted.

projects/simple-nix/shell.nix

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)