Skip to content

Commit

Permalink
Add simple-nix
Browse files Browse the repository at this point in the history
  • Loading branch information
martijnbastiaan committed Nov 28, 2020
1 parent 44e3072 commit d7c5399
Show file tree
Hide file tree
Showing 27 changed files with 1,172 additions and 34 deletions.
13 changes: 6 additions & 7 deletions .ci/publish.sh
Expand Up @@ -4,11 +4,14 @@ set -x
IFS=$'\n\t'

ROOT=$(git rev-parse --show-toplevel)
"${ROOT}"/.ci/install_ssh_keys.sh

export GIT_SSH_COMMAND="ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"

cd "${ROOT}"

# Clone clash-starters and rollback to very first commit
git clone https://github.com/clash-lang/clash-starters.git
git clone git@github.com:clash-lang/clash-starters.git
cd clash-starters
first_commit=$(git rev-list --max-parents=0 --abbrev-commit HEAD)
git reset "${first_commit}" --hard
Expand All @@ -32,9 +35,5 @@ git config --global user.email "devopsXXX@qbaylogic.com"
git add -A
git commit -m "Automated push from clash-lang/stack-templates"

if [[ "$1" == "master" ]]; then
"${ROOT}"/.ci/install_ssh_keys.sh
git remote add origin-ssh git@github.com:clash-lang/clash-starters.git
export GIT_SSH_COMMAND="ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
git push -f origin-ssh "$(git branch --show-current)"
fi
# Push
git push -f
5 changes: 0 additions & 5 deletions .ci/test_projects.sh → .ci/test_projects_cabal.sh
Expand Up @@ -10,11 +10,6 @@ for project in *.hsfiles; do

cd inst

# Build and test with Stack
stack build
stack test
stack run clash -- Example.Project --vhdl

# Build and test with Cabal
cabal update
cabal build
Expand Down
26 changes: 26 additions & 0 deletions .ci/test_projects_nix.sh
@@ -0,0 +1,26 @@
#!/bin/bash
set -euo pipefail
set -x
IFS=$'\n\t'

cd "$(git rev-parse --show-toplevel)"

for project in *.hsfiles; do
nix-shell --pure --packages stack --run "stack new inst \"${project}\""

cd inst

if [[ -f "shell.nix" ]]; then
# Build and test with nix. Note that nix-build already runs the tests,
# but we'd also like to be able to run them using just cabal in a shell.
nix-build
cabal="cabal --write-ghc-environment-files=always"
nix-shell --pure --run "$cabal run clash -- Example.Project --vhdl"
nix-shell --pure --run "$cabal run doctests"
nix-shell --pure --run "$cabal run test-library"
fi

# Clean up
cd ..
rm -rf inst
done
21 changes: 21 additions & 0 deletions .ci/test_projects_stack.sh
@@ -0,0 +1,21 @@
#!/bin/bash
set -euo pipefail
set -x
IFS=$'\n\t'

cd "$(git rev-parse --show-toplevel)"

for project in *.hsfiles; do
stack new inst "${project}"

cd inst

# Build and test with Stack
stack build
stack test
stack run clash -- Example.Project --vhdl

# Clean up
cd ..
rm -rf inst
done
63 changes: 48 additions & 15 deletions .gitlab-ci.yml
@@ -1,26 +1,59 @@
tests:
image: docker.pkg.github.com/clash-lang/stack-templates/focal-ghc-cabal-stack:2020-11-25
stages:
- check
- test
- publish

.common:
image: docker.pkg.github.com/clash-lang/stack-templates/focal-ghc-cabal-stack:2020-11-28
timeout: 2 hours
stage: test
retry:
max: 2
when:
- runner_system_failure
- stuck_or_timeout_failure
tags:
- local
before_script:
- tar -xf cache.tar.zstd -C / || true
after_script:
- bash -c "tar -cf - $(ls -d /root/.cabal /root/.stack /nix) | zstd -T0 -7 > cache.tar.zstd" || true
cache:
key: cabal-stack-$CI_JOB_NAME
when: always
key: $CI_JOB_NAME
paths:
- cache.tar.xz
script:
# Extract cache
- tar -xf cache.tar.xz -C / || true
- cache.tar.zstd

# Tests
# Use ./render and check for changes
render:
extends: .common
stage: check
script:
- .ci/render_and_check.sh
- .ci/test_projects.sh
- .ci/publish.sh ${CI_COMMIT_REF_NAME}

# Save cache
- XZ_OPT="-T 0 -1" tar -Ixz -cf cache.tar.xz /root/.cabal /root/.stack
tags:
- local
# Build and test with Cabal
cabal:
extends: .common
script:
- .ci/test_projects_cabal.sh

# Build and test with Stack
stack:
extends: .common
script:
- .ci/test_projects_stack.sh

# Build and test with Nix
nix:
extends: .common
image: docker.pkg.github.com/clash-lang/stack-templates/nixos-xz-git:2020-11-28
script:
- bash .ci/test_projects_nix.sh

# If all tests pass, publish to 'clash-lang/clash-starters'.
publish:
stage: publish
script:
- .ci/publish.sh
only:
refs:
- master
Expand Up @@ -38,4 +38,4 @@ RUN apt-get update \

# Compression utils
RUN apt-get update \
&& apt-get -y install tar xz-utils zip
&& apt-get -y install tar xz-utils zip zstd
File renamed without changes.
10 changes: 10 additions & 0 deletions docker/nixos-xz-git/Dockerfile
@@ -0,0 +1,10 @@
FROM nixos/nix

# Compression utils
RUN nix-channel --add https://channels.nixos.org/nixos-20.09 nixos \
&& nix-channel --update \
&& nix-env -iA nixos.git \
&& nix-env -iA nixos.gnutar \
&& nix-env -iA nixos.lzma \
&& nix-env -iA nixos.zstd \
&& nix-env -iA nixos.bash
21 changes: 21 additions & 0 deletions docker/nixos-xz-git/build-and-publish-docker-image.sh
@@ -0,0 +1,21 @@
#!/usr/bin/env bash

set -xeo pipefail

REPO="docker.pkg.github.com/clash-lang/stack-templates"
NAME="nixos-xz-git"
DIR=$(dirname "$0")
now=$(date +%F)

docker build -t "${REPO}/${NAME}:$now" "$DIR"
docker tag "${REPO}/${NAME}:$now" "${REPO}/${NAME}:latest"

read -p "Push to GitHub? (y/N) " push

if [[ $push =~ ^[Yy]$ ]]; then
docker push "${REPO}/${NAME}:$now"
docker push "${REPO}/${NAME}:latest"
else
echo "Skipping push to container registry"
fi

1 change: 1 addition & 0 deletions projects/simple-nix/.vscode
51 changes: 51 additions & 0 deletions projects/simple-nix/README.md
@@ -0,0 +1,51 @@
<!-- omit in toc -->
# Simple Nix Starter Project
This starter project contains the scaffolding needed to integrate Clash with the Nix Stack build system. Read [Simple Starter Project](https://github.com/clash-lang/clash-starters/blob/main/simple/README.md) for more information on the various files.

<!-- omit in toc -->
# Table of Contents
- [Getting this project](#getting-this-project)
- [Building and testing this project](#building-and-testing-this-project)
- [REPL](#repl)
- [Adding custom dependencies / updating nix](#adding-custom-dependencies--updating-nix)

# Getting this project
Run `nix-shell --packages stack --run "stack new my-clash-project clash-lang/simple-nix"`.

# Building and testing this project
Build the project with:

```bash
nix-build
```

To test and generate HDL, first open a Nix shell by invoking:

```
nix-shell
```

Then, to run the tests defined in `tests/`:

```bash
cabal run test-library
cabal run doctests
```

To compile the project to VHDL run:

```bash
cabal run clash --write-ghc-environment-files=always -- Example.Project --vhdl
```

You can find the HDL files in `vhdl/`. The source can be found in `src/Example/Project.hs`.

# REPL
Clash offers a [REPL](https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop) as a quick way to try things, similar to Python's `python` or Ruby's `irb`. Open the REPL by invoking:

```
cabal run --write-ghc-environment-files=always clashi
```

# Adding custom dependencies / updating nix
`niv` is available after opening `nix-shell`. See [niv on Hackage](https://hackage.haskell.org/package/niv) for more information.
1 change: 1 addition & 0 deletions projects/simple-nix/Setup.hs
1 change: 1 addition & 0 deletions projects/simple-nix/bin
6 changes: 6 additions & 0 deletions projects/simple-nix/default.nix
@@ -0,0 +1,6 @@
{ nixpkgs ? import ./nix/nixpkgs.nix {} }:

with nixpkgs.pkgs;
with gitignore;

haskellPackages.callCabal2nix "{{name}}" (gitignoreSource ./.) {}
18 changes: 18 additions & 0 deletions projects/simple-nix/nix/nixpkgs.nix
@@ -0,0 +1,18 @@
{ sources ? import ./sources.nix }:

let
overlay = _: pkgs: {

# Nix tooling
niv = (import sources.niv {}).niv;
gitignore = import sources.gitignore { inherit (pkgs) lib; };

# Haskell overrides
haskellPackages = pkgs.haskellPackages.override {
overrides = self: super: {
# Add overrides here
};
};
};

in import sources.nixpkgs { overlays = [ overlay ]; }
38 changes: 38 additions & 0 deletions projects/simple-nix/nix/sources.json
@@ -0,0 +1,38 @@
{
"gitignore": {
"branch": "master",
"description": "Nix function for filtering local git sources",
"homepage": "",
"owner": "hercules-ci",
"repo": "gitignore",
"rev": "c4662e662462e7bf3c2a968483478a665d00e717",
"sha256": "1npnx0h6bd0d7ql93ka7azhj40zgjp815fw2r6smg8ch9p7mzdlx",
"type": "tarball",
"url": "https://github.com/hercules-ci/gitignore/archive/c4662e662462e7bf3c2a968483478a665d00e717.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
},
"niv": {
"branch": "master",
"description": "Easy dependency management for Nix projects",
"homepage": "https://github.com/nmattia/niv",
"owner": "nmattia",
"repo": "niv",
"rev": "ba57d5a29b4e0f2085917010380ef3ddc3cf380f",
"sha256": "1kpsvc53x821cmjg1khvp1nz7906gczq8mp83664cr15h94sh8i4",
"type": "tarball",
"url": "https://github.com/nmattia/niv/archive/ba57d5a29b4e0f2085917010380ef3ddc3cf380f.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
},
"nixpkgs": {
"branch": "nixos-20.09",
"description": "Nix Packages collection",
"homepage": "",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "ffb3aab257e8851b558cdc6079241a7eb0c7239e",
"sha256": "147d4nsq0334skyr9rghcfnizasvba1pr2rq72axwdldlma0qli1",
"type": "tarball",
"url": "https://github.com/NixOS/nixpkgs/archive/ffb3aab257e8851b558cdc6079241a7eb0c7239e.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
}
}

0 comments on commit d7c5399

Please sign in to comment.