diff options
author | Akshay <[email protected]> | 2020-12-01 13:46:34 +0000 |
---|---|---|
committer | Akshay <[email protected]> | 2020-12-01 13:46:34 +0000 |
commit | caf1e7203ccdbef4a1913e475f50f7a2a80e20d4 (patch) | |
tree | cd5cb41d657601cf493167ecb232960d79203f1d |
init
-rw-r--r-- | .gitignore | 3 | ||||
-rw-r--r-- | LICENSE | 20 | ||||
-rw-r--r-- | nix/sources.json | 26 | ||||
-rw-r--r-- | nix/sources.nix | 148 | ||||
-rw-r--r-- | readme.txt | 20 | ||||
-rw-r--r-- | shell.nix | 41 |
6 files changed, 258 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..86a7bac --- /dev/null +++ b/.gitignore | |||
@@ -0,0 +1,3 @@ | |||
1 | result | ||
2 | .envrc | ||
3 | |||
@@ -0,0 +1,20 @@ | |||
1 | Copyright 2020 Akshay Oppiliappan <[email protected]> | ||
2 | |||
3 | Permission is hereby granted, free of charge, to any person obtaining | ||
4 | a copy of this software and associated documentation files (the | ||
5 | "Software"), to deal in the Software without restriction, including | ||
6 | without limitation the rights to use, copy, modify, merge, publish, | ||
7 | distribute, sublicense, and/or sell copies of the Software, and to | ||
8 | permit persons to whom the Software is furnished to do so, subject to | ||
9 | the following conditions: | ||
10 | |||
11 | The above copyright notice and this permission notice shall be | ||
12 | included in all copies or substantial portions of the Software. | ||
13 | |||
14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
diff --git a/nix/sources.json b/nix/sources.json new file mode 100644 index 0000000..9ea73c6 --- /dev/null +++ b/nix/sources.json | |||
@@ -0,0 +1,26 @@ | |||
1 | { | ||
2 | "niv": { | ||
3 | "branch": "master", | ||
4 | "description": "Easy dependency management for Nix projects", | ||
5 | "homepage": "https://github.com/nmattia/niv", | ||
6 | "owner": "nmattia", | ||
7 | "repo": "niv", | ||
8 | "rev": "ba57d5a29b4e0f2085917010380ef3ddc3cf380f", | ||
9 | "sha256": "1kpsvc53x821cmjg1khvp1nz7906gczq8mp83664cr15h94sh8i4", | ||
10 | "type": "tarball", | ||
11 | "url": "https://github.com/nmattia/niv/archive/ba57d5a29b4e0f2085917010380ef3ddc3cf380f.tar.gz", | ||
12 | "url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz" | ||
13 | }, | ||
14 | "nixpkgs": { | ||
15 | "branch": "20.03", | ||
16 | "description": "Nix Packages collection", | ||
17 | "homepage": "", | ||
18 | "owner": "NixOS", | ||
19 | "repo": "nixpkgs", | ||
20 | "rev": "5272327b81ed355bbed5659b8d303cf2979b6953", | ||
21 | "sha256": "0182ys095dfx02vl2a20j1hz92dx3mfgz2a6fhn31bqlp1wa8hlq", | ||
22 | "type": "tarball", | ||
23 | "url": "https://github.com/NixOS/nixpkgs/archive/5272327b81ed355bbed5659b8d303cf2979b6953.tar.gz", | ||
24 | "url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz" | ||
25 | } | ||
26 | } | ||
diff --git a/nix/sources.nix b/nix/sources.nix new file mode 100644 index 0000000..b64b8f8 --- /dev/null +++ b/nix/sources.nix | |||
@@ -0,0 +1,148 @@ | |||
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: spec: | ||
10 | if spec.builtin or true then | ||
11 | builtins_fetchurl { inherit (spec) url sha256; } | ||
12 | else | ||
13 | pkgs.fetchurl { inherit (spec) url sha256; }; | ||
14 | |||
15 | fetch_tarball = pkgs: name: spec: | ||
16 | let | ||
17 | ok = str: ! builtins.isNull (builtins.match "[a-zA-Z0-9+-._?=]" str); | ||
18 | # sanitize the name, though nix will still fail if name starts with period | ||
19 | name' = stringAsChars (x: if ! ok x then "-" else x) "${name}-src"; | ||
20 | in | ||
21 | if spec.builtin or true then | ||
22 | builtins_fetchTarball { name = name'; inherit (spec) url sha256; } | ||
23 | else | ||
24 | pkgs.fetchzip { name = name'; inherit (spec) url sha256; }; | ||
25 | |||
26 | fetch_git = spec: | ||
27 | builtins.fetchGit { url = spec.repo; inherit (spec) rev ref; }; | ||
28 | |||
29 | fetch_local = spec: spec.path; | ||
30 | |||
31 | fetch_builtin-tarball = name: throw | ||
32 | ''[${name}] The niv type "builtin-tarball" is deprecated. You should instead use `builtin = true`. | ||
33 | $ niv modify ${name} -a type=tarball -a builtin=true''; | ||
34 | |||
35 | fetch_builtin-url = name: throw | ||
36 | ''[${name}] The niv type "builtin-url" will soon be deprecated. You should instead use `builtin = true`. | ||
37 | $ niv modify ${name} -a type=file -a builtin=true''; | ||
38 | |||
39 | # | ||
40 | # Various helpers | ||
41 | # | ||
42 | |||
43 | # The set of packages used when specs are fetched using non-builtins. | ||
44 | mkPkgs = sources: | ||
45 | let | ||
46 | sourcesNixpkgs = | ||
47 | import (builtins_fetchTarball { inherit (sources.nixpkgs) url sha256; }) {}; | ||
48 | hasNixpkgsPath = builtins.any (x: x.prefix == "nixpkgs") builtins.nixPath; | ||
49 | hasThisAsNixpkgsPath = <nixpkgs> == ./.; | ||
50 | in | ||
51 | if builtins.hasAttr "nixpkgs" sources | ||
52 | then sourcesNixpkgs | ||
53 | else if hasNixpkgsPath && ! hasThisAsNixpkgsPath then | ||
54 | import <nixpkgs> {} | ||
55 | else | ||
56 | abort | ||
57 | '' | ||
58 | Please specify either <nixpkgs> (through -I or NIX_PATH=nixpkgs=...) or | ||
59 | add a package called "nixpkgs" to your sources.json. | ||
60 | ''; | ||
61 | |||
62 | # The actual fetching function. | ||
63 | fetch = pkgs: name: spec: | ||
64 | |||
65 | if ! builtins.hasAttr "type" spec then | ||
66 | abort "ERROR: niv spec ${name} does not have a 'type' attribute" | ||
67 | else if spec.type == "file" then fetch_file pkgs spec | ||
68 | else if spec.type == "tarball" then fetch_tarball pkgs name spec | ||
69 | else if spec.type == "git" then fetch_git spec | ||
70 | else if spec.type == "local" then fetch_local spec | ||
71 | else if spec.type == "builtin-tarball" then fetch_builtin-tarball name | ||
72 | else if spec.type == "builtin-url" then fetch_builtin-url name | ||
73 | else | ||
74 | abort "ERROR: niv spec ${name} has unknown type ${builtins.toJSON spec.type}"; | ||
75 | |||
76 | # If the environment variable NIV_OVERRIDE_${name} is set, then use | ||
77 | # the path directly as opposed to the fetched source. | ||
78 | replace = name: drv: | ||
79 | let | ||
80 | saneName = stringAsChars (c: if isNull (builtins.match "[a-zA-Z0-9]" c) then "_" else c) name; | ||
81 | ersatz = builtins.getEnv "NIV_OVERRIDE_${saneName}"; | ||
82 | in | ||
83 | if ersatz == "" then drv else ersatz; | ||
84 | |||
85 | # Ports of functions for older nix versions | ||
86 | |||
87 | # a Nix version of mapAttrs if the built-in doesn't exist | ||
88 | mapAttrs = builtins.mapAttrs or ( | ||
89 | f: set: with builtins; | ||
90 | listToAttrs (map (attr: { name = attr; value = f attr set.${attr}; }) (attrNames set)) | ||
91 | ); | ||
92 | |||
93 | # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/lists.nix#L295 | ||
94 | range = first: last: if first > last then [] else builtins.genList (n: first + n) (last - first + 1); | ||
95 | |||
96 | # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L257 | ||
97 | stringToCharacters = s: map (p: builtins.substring p 1 s) (range 0 (builtins.stringLength s - 1)); | ||
98 | |||
99 | # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L269 | ||
100 | stringAsChars = f: s: concatStrings (map f (stringToCharacters s)); | ||
101 | concatStrings = builtins.concatStringsSep ""; | ||
102 | |||
103 | # fetchTarball version that is compatible between all the versions of Nix | ||
104 | builtins_fetchTarball = { url, name, sha256 }@attrs: | ||
105 | let | ||
106 | inherit (builtins) lessThan nixVersion fetchTarball; | ||
107 | in | ||
108 | if lessThan nixVersion "1.12" then | ||
109 | fetchTarball { inherit name url; } | ||
110 | else | ||
111 | fetchTarball attrs; | ||
112 | |||
113 | # fetchurl version that is compatible between all the versions of Nix | ||
114 | builtins_fetchurl = { url, sha256 }@attrs: | ||
115 | let | ||
116 | inherit (builtins) lessThan nixVersion fetchurl; | ||
117 | in | ||
118 | if lessThan nixVersion "1.12" then | ||
119 | fetchurl { inherit url; } | ||
120 | else | ||
121 | fetchurl attrs; | ||
122 | |||
123 | # Create the final "sources" from the config | ||
124 | mkSources = config: | ||
125 | mapAttrs ( | ||
126 | name: spec: | ||
127 | if builtins.hasAttr "outPath" spec | ||
128 | then abort | ||
129 | "The values in sources.json should not have an 'outPath' attribute" | ||
130 | else | ||
131 | spec // { outPath = replace name (fetch config.pkgs name spec); } | ||
132 | ) config.sources; | ||
133 | |||
134 | # The "config" used by the fetchers | ||
135 | mkConfig = | ||
136 | { sourcesFile ? if builtins.pathExists ./sources.json then ./sources.json else null | ||
137 | , sources ? if isNull sourcesFile then {} else builtins.fromJSON (builtins.readFile sourcesFile) | ||
138 | , pkgs ? mkPkgs sources | ||
139 | }: rec { | ||
140 | # The sources, i.e. the attribute set of spec name to spec | ||
141 | inherit sources; | ||
142 | |||
143 | # The "pkgs" (evaluated nixpkgs) to use for e.g. non-builtin fetchers | ||
144 | inherit pkgs; | ||
145 | }; | ||
146 | |||
147 | in | ||
148 | mkSources (mkConfig {}) // { __functor = _: settings: mkSources (mkConfig settings); } | ||
diff --git a/readme.txt b/readme.txt new file mode 100644 index 0000000..49ff08a --- /dev/null +++ b/readme.txt | |||
@@ -0,0 +1,20 @@ | |||
1 | advent of code | ||
2 | -------------- | ||
3 | |||
4 | some solutions to aoc 2020, written in haskell. | ||
5 | |||
6 | |||
7 | build | ||
8 | ----- | ||
9 | |||
10 | each day's solution is a separate cabal executable: | ||
11 | |||
12 | |||
13 | cabal run DayXX | ||
14 | |||
15 | |||
16 | or with nix: | ||
17 | |||
18 | |||
19 | nix-build | ||
20 | ./result/bin/DayXX | ||
diff --git a/shell.nix b/shell.nix new file mode 100644 index 0000000..f1d55b5 --- /dev/null +++ b/shell.nix | |||
@@ -0,0 +1,41 @@ | |||
1 | let | ||
2 | sources = import ./nix/sources.nix; | ||
3 | pkgs = import sources.nixpkgs {}; | ||
4 | |||
5 | inherit (pkgs) haskellPackages; | ||
6 | all-hls = pkgs.fetchFromGitHub { | ||
7 | owner = "masaeedu"; | ||
8 | repo = "all-hls"; | ||
9 | rev = "155e57d7ca9f79ce293360f98895e9bd68d12355"; | ||
10 | sha256 = "04s3mrxjdr7gmd901l1z23qglqmn8i39v7sdf2fv4zbv6hz24ydb"; | ||
11 | }; | ||
12 | |||
13 | hls = import all-hls { | ||
14 | platform = "Linux"; | ||
15 | version = "0.4.0"; | ||
16 | ghc = "8.6.5"; | ||
17 | }; # All parameters are optional. The default values are shown here. | ||
18 | |||
19 | |||
20 | haskellDeps = ps: with ps; [ | ||
21 | base | ||
22 | lens | ||
23 | parsec | ||
24 | mtl | ||
25 | ]; | ||
26 | |||
27 | ghc = haskellPackages.ghcWithPackages haskellDeps; | ||
28 | |||
29 | externalPackages = [ | ||
30 | ghc | ||
31 | pkgs.gdb | ||
32 | pkgs.cabal2nix | ||
33 | haskellPackages.cabal-install | ||
34 | haskellPackages.hoogle | ||
35 | haskellPackages.hlint | ||
36 | ]; | ||
37 | in | ||
38 | pkgs.stdenv.mkDerivation { | ||
39 | name = "aoc"; | ||
40 | buildInputs = externalPackages ++ [ hls ]; | ||
41 | } | ||