Skip to content

Commit 3a0e344

Browse files
committed
work-in-progress Nix support
with https://github.com/input-output-hk/haskell.nix
1 parent 26bdd60 commit 3a0e344

File tree

6 files changed

+257
-2
lines changed

6 files changed

+257
-2
lines changed

Diff for: .github/workflows/ci.yml renamed to .github/workflows/Cabal.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# modified from https://github.com/jgm/pandoc/blob/master/.github/workflows/ci.yml
2-
name: CI
2+
name: Cabal
33

44
on:
55
push:

Diff for: .github/workflows/Nix.yml

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Nix
2+
3+
on:
4+
push:
5+
branches:
6+
- '**'
7+
paths-ignore: []
8+
pull_request:
9+
paths-ignore: []
10+
11+
jobs:
12+
linux:
13+
14+
runs-on: ubuntu-20.04
15+
strategy:
16+
fail-fast: false
17+
steps:
18+
- uses: actions/checkout@v2
19+
20+
# https://github.com/marketplace/actions/install-nix
21+
- name: Install Nix
22+
uses: cachix/install-nix-action@v14
23+
with:
24+
extra_nix_config: |
25+
trusted-public-keys = hydra.iohk.io:f/Ea+s+dFdN+3Y/G+FDgSq+a5NEWhJGzdjvKNGv0/EQ= cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY=
26+
substituters = https://hydra.iohk.io https://cache.nixos.org/
27+
- run: nix-build

Diff for: README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ CodeWorld
22
=========
33

44
[![Build Status](https://travis-ci.org/google/codeworld.svg?branch=master)](https://travis-ci.org/google/codeworld)
5-
[![Build status](https://github.com/google/codeworld/actions/workflows/ci.yml/badge.svg)](https://github.com/google/codeworld/actions/workflows/ci.yml)
5+
[![Build status](https://github.com/google/codeworld/actions/workflows/Cabal.yml/badge.svg)](https://github.com/google/codeworld/actions/workflows/Cabal.yml)
6+
[![Build status](https://github.com/google/codeworld/actions/workflows/Nix.yml/badge.svg)](https://github.com/google/codeworld/actions/workflows/Nix.yml)
67

78
CodeWorld is an educational environment using Haskell. It provides a simple
89
mathematical model for geometric figures, animations, and interactive and

Diff for: default.nix

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# https://input-output-hk.github.io/haskell.nix/tutorials/getting-started/
2+
let
3+
sources = import ./nix/sources.nix {};
4+
haskellNix = import sources.haskellNix {};
5+
pkgs = import
6+
haskellNix.sources.nixpkgs-2009
7+
haskellNix.nixpkgsArgs;
8+
in pkgs.pkgsCross.ghcjs.haskell-nix.project {
9+
projectFileName = "cabal.project";
10+
src = pkgs.haskell-nix.haskellLib.cleanGit {
11+
name = "haskell-nix-project";
12+
src = ./.;
13+
};
14+
compiler-nix-name = "ghc865";
15+
}

Diff for: nix/sources.json

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"haskellNix": {
3+
"branch": "master",
4+
"description": "Alternative Haskell Infrastructure for Nixpkgs",
5+
"homepage": "https://input-output-hk.github.io/haskell.nix",
6+
"owner": "input-output-hk",
7+
"repo": "haskell.nix",
8+
"rev": "903bdfe655b644690d4b4c6ca4adf55be95377f8",
9+
"sha256": "1skdd228mjcji3k4rkf9n4kwjgvm7kd9r4bakd4lc2igqxv0nzjr",
10+
"type": "tarball",
11+
"url": "https://github.com/input-output-hk/haskell.nix/archive/903bdfe655b644690d4b4c6ca4adf55be95377f8.tar.gz",
12+
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
13+
},
14+
"niv": {
15+
"branch": "master",
16+
"description": "Easy dependency management for Nix projects",
17+
"homepage": "https://github.com/nmattia/niv",
18+
"owner": "nmattia",
19+
"repo": "niv",
20+
"rev": "e0ca65c81a2d7a4d82a189f1e23a48d59ad42070",
21+
"sha256": "1pq9nh1d8nn3xvbdny8fafzw87mj7gsmp6pxkdl65w2g18rmcmzx",
22+
"type": "tarball",
23+
"url": "https://github.com/nmattia/niv/archive/e0ca65c81a2d7a4d82a189f1e23a48d59ad42070.tar.gz",
24+
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
25+
},
26+
"nixpkgs": {
27+
"branch": "release-20.03",
28+
"description": "Nix Packages collection",
29+
"homepage": "",
30+
"owner": "NixOS",
31+
"repo": "nixpkgs",
32+
"rev": "eb73405ecceb1dc505b7cbbd234f8f94165e2696",
33+
"sha256": "06k21wbyhhvq2f1xczszh3c2934p0m02by3l2ixvd6nkwrqklax7",
34+
"type": "tarball",
35+
"url": "https://github.com/NixOS/nixpkgs/archive/eb73405ecceb1dc505b7cbbd234f8f94165e2696.tar.gz",
36+
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
37+
}
38+
}

Diff for: nix/sources.nix

+174
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
# This file has been generated by Niv.
2+
3+
let
4+
5+
#
6+
# The fetchers. fetch_<type> fetches specs of type <type>.
7+
#
8+
9+
fetch_file = pkgs: name: spec:
10+
let
11+
name' = sanitizeName name + "-src";
12+
in
13+
if spec.builtin or true then
14+
builtins_fetchurl { inherit (spec) url sha256; name = name'; }
15+
else
16+
pkgs.fetchurl { inherit (spec) url sha256; name = name'; };
17+
18+
fetch_tarball = pkgs: name: spec:
19+
let
20+
name' = sanitizeName name + "-src";
21+
in
22+
if spec.builtin or true then
23+
builtins_fetchTarball { name = name'; inherit (spec) url sha256; }
24+
else
25+
pkgs.fetchzip { name = name'; inherit (spec) url sha256; };
26+
27+
fetch_git = name: spec:
28+
let
29+
ref =
30+
if spec ? ref then spec.ref else
31+
if spec ? branch then "refs/heads/${spec.branch}" else
32+
if spec ? tag then "refs/tags/${spec.tag}" else
33+
abort "In git source '${name}': Please specify `ref`, `tag` or `branch`!";
34+
in
35+
builtins.fetchGit { url = spec.repo; inherit (spec) rev; inherit ref; };
36+
37+
fetch_local = spec: spec.path;
38+
39+
fetch_builtin-tarball = name: throw
40+
''[${name}] The niv type "builtin-tarball" is deprecated. You should instead use `builtin = true`.
41+
$ niv modify ${name} -a type=tarball -a builtin=true'';
42+
43+
fetch_builtin-url = name: throw
44+
''[${name}] The niv type "builtin-url" will soon be deprecated. You should instead use `builtin = true`.
45+
$ niv modify ${name} -a type=file -a builtin=true'';
46+
47+
#
48+
# Various helpers
49+
#
50+
51+
# https://github.com/NixOS/nixpkgs/pull/83241/files#diff-c6f540a4f3bfa4b0e8b6bafd4cd54e8bR695
52+
sanitizeName = name:
53+
(
54+
concatMapStrings (s: if builtins.isList s then "-" else s)
55+
(
56+
builtins.split "[^[:alnum:]+._?=-]+"
57+
((x: builtins.elemAt (builtins.match "\\.*(.*)" x) 0) name)
58+
)
59+
);
60+
61+
# The set of packages used when specs are fetched using non-builtins.
62+
mkPkgs = sources: system:
63+
let
64+
sourcesNixpkgs =
65+
import (builtins_fetchTarball { inherit (sources.nixpkgs) url sha256; }) { inherit system; };
66+
hasNixpkgsPath = builtins.any (x: x.prefix == "nixpkgs") builtins.nixPath;
67+
hasThisAsNixpkgsPath = <nixpkgs> == ./.;
68+
in
69+
if builtins.hasAttr "nixpkgs" sources
70+
then sourcesNixpkgs
71+
else if hasNixpkgsPath && ! hasThisAsNixpkgsPath then
72+
import <nixpkgs> {}
73+
else
74+
abort
75+
''
76+
Please specify either <nixpkgs> (through -I or NIX_PATH=nixpkgs=...) or
77+
add a package called "nixpkgs" to your sources.json.
78+
'';
79+
80+
# The actual fetching function.
81+
fetch = pkgs: name: spec:
82+
83+
if ! builtins.hasAttr "type" spec then
84+
abort "ERROR: niv spec ${name} does not have a 'type' attribute"
85+
else if spec.type == "file" then fetch_file pkgs name spec
86+
else if spec.type == "tarball" then fetch_tarball pkgs name spec
87+
else if spec.type == "git" then fetch_git name spec
88+
else if spec.type == "local" then fetch_local spec
89+
else if spec.type == "builtin-tarball" then fetch_builtin-tarball name
90+
else if spec.type == "builtin-url" then fetch_builtin-url name
91+
else
92+
abort "ERROR: niv spec ${name} has unknown type ${builtins.toJSON spec.type}";
93+
94+
# If the environment variable NIV_OVERRIDE_${name} is set, then use
95+
# the path directly as opposed to the fetched source.
96+
replace = name: drv:
97+
let
98+
saneName = stringAsChars (c: if isNull (builtins.match "[a-zA-Z0-9]" c) then "_" else c) name;
99+
ersatz = builtins.getEnv "NIV_OVERRIDE_${saneName}";
100+
in
101+
if ersatz == "" then drv else
102+
# this turns the string into an actual Nix path (for both absolute and
103+
# relative paths)
104+
if builtins.substring 0 1 ersatz == "/" then /. + ersatz else /. + builtins.getEnv "PWD" + "/${ersatz}";
105+
106+
# Ports of functions for older nix versions
107+
108+
# a Nix version of mapAttrs if the built-in doesn't exist
109+
mapAttrs = builtins.mapAttrs or (
110+
f: set: with builtins;
111+
listToAttrs (map (attr: { name = attr; value = f attr set.${attr}; }) (attrNames set))
112+
);
113+
114+
# https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/lists.nix#L295
115+
range = first: last: if first > last then [] else builtins.genList (n: first + n) (last - first + 1);
116+
117+
# https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L257
118+
stringToCharacters = s: map (p: builtins.substring p 1 s) (range 0 (builtins.stringLength s - 1));
119+
120+
# https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L269
121+
stringAsChars = f: s: concatStrings (map f (stringToCharacters s));
122+
concatMapStrings = f: list: concatStrings (map f list);
123+
concatStrings = builtins.concatStringsSep "";
124+
125+
# https://github.com/NixOS/nixpkgs/blob/8a9f58a375c401b96da862d969f66429def1d118/lib/attrsets.nix#L331
126+
optionalAttrs = cond: as: if cond then as else {};
127+
128+
# fetchTarball version that is compatible between all the versions of Nix
129+
builtins_fetchTarball = { url, name ? null, sha256 }@attrs:
130+
let
131+
inherit (builtins) lessThan nixVersion fetchTarball;
132+
in
133+
if lessThan nixVersion "1.12" then
134+
fetchTarball ({ inherit url; } // (optionalAttrs (!isNull name) { inherit name; }))
135+
else
136+
fetchTarball attrs;
137+
138+
# fetchurl version that is compatible between all the versions of Nix
139+
builtins_fetchurl = { url, name ? null, sha256 }@attrs:
140+
let
141+
inherit (builtins) lessThan nixVersion fetchurl;
142+
in
143+
if lessThan nixVersion "1.12" then
144+
fetchurl ({ inherit url; } // (optionalAttrs (!isNull name) { inherit name; }))
145+
else
146+
fetchurl attrs;
147+
148+
# Create the final "sources" from the config
149+
mkSources = config:
150+
mapAttrs (
151+
name: spec:
152+
if builtins.hasAttr "outPath" spec
153+
then abort
154+
"The values in sources.json should not have an 'outPath' attribute"
155+
else
156+
spec // { outPath = replace name (fetch config.pkgs name spec); }
157+
) config.sources;
158+
159+
# The "config" used by the fetchers
160+
mkConfig =
161+
{ sourcesFile ? if builtins.pathExists ./sources.json then ./sources.json else null
162+
, sources ? if isNull sourcesFile then {} else builtins.fromJSON (builtins.readFile sourcesFile)
163+
, system ? builtins.currentSystem
164+
, pkgs ? mkPkgs sources system
165+
}: rec {
166+
# The sources, i.e. the attribute set of spec name to spec
167+
inherit sources;
168+
169+
# The "pkgs" (evaluated nixpkgs) to use for e.g. non-builtin fetchers
170+
inherit pkgs;
171+
};
172+
173+
in
174+
mkSources (mkConfig {}) // { __functor = _: settings: mkSources (mkConfig settings); }

0 commit comments

Comments
 (0)