trying a thing

This commit is contained in:
Tom Sydney Kerckhove 2021-10-20 17:59:50 +02:00
commit 9b57c76cd8
30 changed files with 810 additions and 0 deletions

4
.github/FUNDING.yml vendored Normal file
View File

@ -0,0 +1,4 @@
github: NorfairKing
patreon: NorfairKing
liberapay: NorfairKing
custom: https://cs-syd.eu/support

20
.github/workflows/nix-build.yml vendored Normal file
View File

@ -0,0 +1,20 @@
name: "Nix Build"
on:
pull_request:
push:
jobs:
tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: cachix/install-nix-action@v12
- name: free disk space
run: |
sudo rm -rf /opt
- uses: cachix/cachix-action@v8
with:
name: yamlparse
extraPullNames: validity,safe-coloured-text,sydtest
signingKey: '${{ secrets.CACHIX_SIGNING_KEY }}'
- run: nix-build default.nix
- run: nix-build ci.nix

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
.stack-work
*.yaml.lock
result*
.pre-commit-config.yaml

21
LICENSE Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2020 Tom Sydney Kerckhove
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

9
README.md Normal file
View File

@ -0,0 +1,9 @@
# Autodocodec
Autodocodec is short for "self(auto)- documenting encoder and decoder".
## Goals:
* Correct-by-construction encoding and decoding, without generating code.
* Generate automatically-correct documentation from code.
* Fun but not important: Be able to provide instances without depending on aeson and/or yaml.

2
autodocodec-aeson/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.stack-work/
*~

View File

@ -0,0 +1,31 @@
# Changelog
## [Unreleased]
### Added
* `FieldParserOptional` and `FieldParserOptionalWithDefault`
### Changed
* `FieldParserOptional` is now called `FieldParserOptionalOrNull` and
`FieldParserOptionalWithDefault` is now called `FieldParserOptionalOrNullWithDefault`, but
if you don't use these constructors explicitly, no behaviour has changed.
## [0.1.0.2] - 2020-10-12
### Added
* `prettyColourisedSchemaDoc`
* `prettyColourisedParserDoc`
* `prettyColourisedSchema`
* `confDescWith`
## [0.1.0.1] - 2020-05-10
### Added
* `prettySchemaDoc`
* `prettyParserDoc`

21
autodocodec-aeson/LICENSE Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2020-2021 Tom Sydney Kerckhove
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@ -0,0 +1,56 @@
cabal-version: 1.12
-- This file has been generated from package.yaml by hpack version 0.34.4.
--
-- see: https://github.com/sol/hpack
name: autodocodec-aeson
version: 0.0.0.0
synopsis: Self-documenting encoder and decoder for aeson
homepage: https://github.com/NorfairKing/autodocodec#readme
bug-reports: https://github.com/NorfairKing/autodocodec/issues
author: Tom Sydney Kerckhove
maintainer: syd@cs-syd.eu
copyright: 2021 Tom Sydney Kerckhove
license: MIT
license-file: LICENSE
build-type: Simple
source-repository head
type: git
location: https://github.com/NorfairKing/autodocodec
library
exposed-modules:
Autodocodec.Aeson
other-modules:
Paths_autodocodec_aeson
hs-source-dirs:
src
build-depends:
aeson
, base >=4.7 && <5
, bytestring
, text
default-language: Haskell2010
test-suite autodocodec-aeson-test
type: exitcode-stdio-1.0
main-is: Spec.hs
other-modules:
Autodocodec.AesonSpec
Paths_autodocodec_aeson
hs-source-dirs:
test/
ghc-options: -threaded -rtsopts -with-rtsopts=-N -Wall
build-tool-depends:
sydtest-discover:sydtest-discover
build-depends:
aeson
, autodocodec
, autodocodec-aeson
, base >=4.7 && <5
, bytestring
, sydtest
, text
default-language: Haskell2010

View File

@ -0,0 +1,39 @@
name: autodocodec-aeson
version: 0.0.0.0
github: "NorfairKing/autodocodec"
license: MIT
license-file: LICENSE
author: "Tom Sydney Kerckhove"
maintainer: "syd@cs-syd.eu"
copyright: "2021 Tom Sydney Kerckhove"
synopsis: Self-documenting encoder and decoder for aeson
dependencies:
- base >= 4.7 && < 5
library:
source-dirs: src
dependencies:
- aeson
- autodocodec
- bytestring
- text
tests:
autodocodec-aeson-test:
main: Spec.hs
source-dirs: test/
build-tools: sydtest-discover
ghc-options:
- -threaded
- -rtsopts
- -with-rtsopts=-N
- -Wall
dependencies:
- text
- sydtest
- bytestring
- aeson
- autodocodec-aeson
- autodocodec

View File

@ -0,0 +1 @@
module Autodocodec.Aeson where

View File

@ -0,0 +1,6 @@
module Autodocodec.AesonSpec (spec) where
import Test.Syd
spec :: Spec
spec = pure ()

View File

@ -0,0 +1 @@
{-# OPTIONS_GHC -F -pgmF sydtest-discover #-}

2
autodocodec/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.stack-work/
*~

31
autodocodec/Changelog.md Normal file
View File

@ -0,0 +1,31 @@
# Changelog
## [Unreleased]
### Added
* `FieldParserOptional` and `FieldParserOptionalWithDefault`
### Changed
* `FieldParserOptional` is now called `FieldParserOptionalOrNull` and
`FieldParserOptionalWithDefault` is now called `FieldParserOptionalOrNullWithDefault`, but
if you don't use these constructors explicitly, no behaviour has changed.
## [0.1.0.2] - 2020-10-12
### Added
* `prettyColourisedSchemaDoc`
* `prettyColourisedParserDoc`
* `prettyColourisedSchema`
* `confDescWith`
## [0.1.0.1] - 2020-05-10
### Added
* `prettySchemaDoc`
* `prettyParserDoc`

21
autodocodec/LICENSE Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2020-2021 Tom Sydney Kerckhove
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@ -0,0 +1,36 @@
cabal-version: 1.12
-- This file has been generated from package.yaml by hpack version 0.34.4.
--
-- see: https://github.com/sol/hpack
name: autodocodec
version: 0.0.0.0
synopsis: Self-documenting encoder and decoder
homepage: https://github.com/NorfairKing/autodocodec#readme
bug-reports: https://github.com/NorfairKing/autodocodec/issues
author: Tom Sydney Kerckhove
maintainer: syd@cs-syd.eu
copyright: 2021 Tom Sydney Kerckhove
license: MIT
license-file: LICENSE
build-type: Simple
source-repository head
type: git
location: https://github.com/NorfairKing/autodocodec
library
exposed-modules:
Autodocodec
Autodocodec.Class
Autodocodec.Codec
other-modules:
Paths_autodocodec
hs-source-dirs:
src
build-depends:
base >=4.7 && <5
, bytestring
, text
default-language: Haskell2010

18
autodocodec/package.yaml Normal file
View File

@ -0,0 +1,18 @@
name: autodocodec
version: 0.0.0.0
github: "NorfairKing/autodocodec"
license: MIT
license-file: LICENSE
author: "Tom Sydney Kerckhove"
maintainer: "syd@cs-syd.eu"
copyright: "2021 Tom Sydney Kerckhove"
synopsis: Self-documenting encoder and decoder
dependencies:
- base >= 4.7 && < 5
library:
source-dirs: src
dependencies:
- bytestring
- text

View File

@ -0,0 +1,9 @@
module Autodocodec
( -- * To make sure we definitely export everything
module Autodocodec.Class,
module Autodocodec.Codec,
)
where
import Autodocodec.Class
import Autodocodec.Codec

View File

@ -0,0 +1,12 @@
{-# LANGUAGE FlexibleInstances #-}
module Autodocodec.Class where
import Autodocodec.Codec
import Data.Text (Text)
class HasCodec a where
codec :: Codec a a
field :: HasCodec output => Text -> ObjectCodec output output
field k = KeyCodec k codec

View File

@ -0,0 +1,68 @@
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Autodocodec.Codec where
import Data.Text (Text)
data Codec input output where
NullCodec :: Codec () ()
BoolCodec :: Codec Bool Bool
StringCodec :: Codec Text Text
ObjectCodec :: ObjectCodec value value -> Codec value value
PureCodec :: output -> Codec input output
BimapCodec :: (oldOutput -> newOutput) -> (newInput -> oldInput) -> Codec oldInput oldOutput -> Codec newInput newOutput
fmapCodec :: (oldOutput -> newOutput) -> Codec input oldOutput -> Codec input newOutput
fmapCodec f = BimapCodec f id
comapCodec :: (newInput -> oldInput) -> Codec oldInput output -> Codec newInput output
comapCodec g = BimapCodec id g
bimapCodec :: (oldOutput -> newOutput) -> (newInput -> oldInput) -> Codec oldInput oldOutput -> Codec newInput newOutput
bimapCodec = BimapCodec
instance Functor (Codec input) where
fmap = fmapCodec
data ObjectCodec input output where
KeyCodec :: Text -> Codec input output -> ObjectCodec input output
PureObjectCodec :: output -> ObjectCodec input output
BimapObjectCodec :: (oldOutput -> newOutput) -> (newInput -> oldInput) -> ObjectCodec oldInput oldOutput -> ObjectCodec newInput newOutput
ApObjectCodec :: ObjectCodec input (output -> newOutput) -> ObjectCodec input output -> ObjectCodec input newOutput
fmapObjectCodec :: (oldOutput -> newOutput) -> ObjectCodec input oldOutput -> ObjectCodec input newOutput
fmapObjectCodec f = BimapObjectCodec f id
comapObjectCodec :: (newInput -> oldInput) -> ObjectCodec oldInput output -> ObjectCodec newInput output
comapObjectCodec g = BimapObjectCodec id g
bimapObjectCodec :: (oldOutput -> newOutput) -> (newInput -> oldInput) -> ObjectCodec oldInput oldOutput -> ObjectCodec newInput newOutput
bimapObjectCodec = BimapObjectCodec
instance Functor (ObjectCodec input) where
fmap = fmapObjectCodec
instance Applicative (ObjectCodec input) where
pure = pureObjectCodec
(<*>) = apObjectCodec
pureObjectCodec :: output -> ObjectCodec input output
pureObjectCodec = PureObjectCodec
apObjectCodec :: ObjectCodec input (output -> newOutput) -> ObjectCodec input output -> ObjectCodec input newOutput
apObjectCodec = ApObjectCodec
(.=) :: ObjectCodec oldInput output -> (newInput -> oldInput) -> ObjectCodec newInput output
(.=) = flip comapObjectCodec
boolCodec :: Codec Bool Bool
boolCodec = BoolCodec
textCodec :: Codec Text Text
textCodec = StringCodec
object :: ObjectCodec value value -> Codec value value
object = ObjectCodec

24
ci.nix Normal file
View File

@ -0,0 +1,24 @@
let
sources = import ./nix/sources.nix;
versions = {
"lts-13_19" = "82d2c663b4dffbd635ed694bcc301284987b8097";
"lts-14_23" = "a87b506140a7267477103759c3f8da5b2e8d994e";
"lts-15_03" = "beeb24f1e939be7d85fdd64e31f13b8fe8238150";
"lts-16_11" = "89db531aea80df58584c9a9e3504ffd9617e6b48";
};
mkReleaseForVersion = version: rev:
let
pkgsf = builtins.fetchGit {
url = "https://github.com/NixOS/nixpkgs";
inherit rev;
};
p = import ./nix/pkgs.nix { inherit sources; inherit pkgsf; };
in
p.yamlparseApplicativeRelease.overrideAttrs (old: { name = "yamlparse-applicative-release-${version}"; });
in
{
release = (import ./nix/pkgs.nix { inherit sources; }).yamlparseApplicativeRelease;
pre-commit-check = (import ./nix/pre-commit.nix { }).run;
} // builtins.mapAttrs mkReleaseForVersion versions

4
default.nix Normal file
View File

@ -0,0 +1,4 @@
let
pkgs = import ./nix/pkgs.nix { };
in
pkgs.yamlparseApplicativePackages

29
nix/overlay.nix Normal file
View File

@ -0,0 +1,29 @@
final:
previous:
with final.haskell.lib;
{
yamlparseApplicativePackages =
let
yamlparseApplicativePkg = name:
doBenchmark (buildStrictly (final.haskellPackages.callCabal2nixWithOptions name (final.gitignoreSource (../. + "/${name}")) "--no-hpack" { }));
in
final.lib.genAttrs [
"yamlparse-applicative"
"yamlparse-applicative-demo"
]
yamlparseApplicativePkg;
yamlparseApplicativeRelease =
final.symlinkJoin {
name = "yamlparse-applicative-release";
paths = final.lib.attrValues final.yamlparseApplicativePackages;
};
haskellPackages = previous.haskellPackages.override (
old: {
overrides = final.lib.composeExtensions (old.overrides or (_: _: { })) (
self: super: final.yamlparseApplicativePackages
);
}
);
}

17
nix/pkgs.nix Normal file
View File

@ -0,0 +1,17 @@
{ sources ? import ./sources.nix
, pkgsf ? sources.nixpkgs
}:
let
pkgs =
import pkgsf {
overlays = [
(import (sources.validity + "/nix/overlay.nix"))
(import (sources.safe-coloured-text + "/nix/overlay.nix"))
(import (sources.sydtest + "/nix/overlay.nix"))
(final: previous: { inherit (import sources.gitignore { inherit (final) lib; }) gitignoreSource; })
(import ./overlay.nix)
];
config.allowUnfree = true;
};
in
pkgs

21
nix/pre-commit.nix Normal file
View File

@ -0,0 +1,21 @@
{ sources ? import ./sources.nix }:
let
pre-commit-hooks = import sources.pre-commit-hooks;
in
{
run = pre-commit-hooks.run {
src = ../.;
hooks = {
hlint.enable = true;
hpack.enable = true;
nixpkgs-fmt.enable = true;
ormolu.enable = true;
};
};
tools = with pre-commit-hooks; [
hlint
hpack
nixpkgs-fmt
ormolu
];
}

86
nix/sources.json Normal file
View File

@ -0,0 +1,86 @@
{
"gitignore": {
"branch": "master",
"description": "Nix function for filtering local git sources",
"homepage": "",
"owner": "hercules-ci",
"repo": "gitignore.nix",
"rev": "211907489e9f198594c0eb0ca9256a1949c9d412",
"sha256": "06j7wpvj54khw0z10fjyi31kpafkr6hi1k0di13k1xp8kywvfyx8",
"type": "tarball",
"url": "https://github.com/hercules-ci/gitignore.nix/archive/211907489e9f198594c0eb0ca9256a1949c9d412.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": "94080ae8286024820c570a2a24ed7c36d7ad04a9",
"sha256": "0wlk52zwlrq727x3z1vg9d9qq4zw62ab5jzg4068iqb6hyb0cr0w",
"type": "tarball",
"url": "https://github.com/nmattia/niv/archive/94080ae8286024820c570a2a24ed7c36d7ad04a9.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
},
"nixpkgs": {
"branch": "nixos-21.05",
"description": "Nix Packages collection",
"homepage": "",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "aa576357673d609e618d87db43210e49d4bb1789",
"sha256": "1868s3mp0lwg1jpxsgmgijzddr90bjkncf6k6zhdjqihf0i1n2np",
"type": "tarball",
"url": "https://github.com/NixOS/nixpkgs/archive/aa576357673d609e618d87db43210e49d4bb1789.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
},
"pre-commit-hooks": {
"branch": "master",
"description": "Seamless integration of https://pre-commit.com git hooks with Nix.",
"homepage": "",
"owner": "cachix",
"repo": "pre-commit-hooks.nix",
"rev": "2e7fac06108b4fc81f5ff9ed9a02bc4f6ede7001",
"sha256": "17xlb7cb1n6cyw8my6ks4iacrz49a6wfqrdj2gvjc52qc91wxvj7",
"type": "tarball",
"url": "https://github.com/cachix/pre-commit-hooks.nix/archive/2e7fac06108b4fc81f5ff9ed9a02bc4f6ede7001.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
},
"safe-coloured-text": {
"branch": "master",
"description": null,
"homepage": "https://cs-syd.eu/posts/2021-03-07-safe-coloured-text",
"owner": "NorfairKing",
"repo": "safe-coloured-text",
"rev": "d5788740052925aebf1dec1cf1669f66d3a35120",
"sha256": "0mrrfnvvwvdq2cf2figvan8z4jk0bypa0xmg11n3af6vsjryih43",
"type": "tarball",
"url": "https://github.com/NorfairKing/safe-coloured-text/archive/d5788740052925aebf1dec1cf1669f66d3a35120.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
},
"sydtest": {
"branch": "master",
"description": "A modern testing framework for Haskell with good defaults and advanced testing features.",
"homepage": "",
"owner": "NorfairKing",
"repo": "sydtest",
"rev": "0125c6d6d17d44753c793adc8dcaa4284fef2b7d",
"sha256": "1mxvwngv7md0z7jddn4ffpqffs6qp8ka3ffbw3v545q5aijnkbhy",
"type": "tarball",
"url": "https://github.com/NorfairKing/sydtest/archive/0125c6d6d17d44753c793adc8dcaa4284fef2b7d.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
},
"validity": {
"branch": "master",
"description": "Validity and validity-based testing",
"homepage": "https://www.youtube.com/watch?v=eIs9qNh17SM",
"owner": "NorfairKing",
"repo": "validity",
"rev": "549f92de456dd7fbf21b84f2f6fdca9a3c6506d9",
"sha256": "1gbs61kcpyd8nha1g13dsmkhslkbmhxyjfb5fmg2nwqayxvdha58",
"type": "tarball",
"url": "https://github.com/NorfairKing/validity/archive/549f92de456dd7fbf21b84f2f6fdca9a3c6506d9.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
}
}

177
nix/sources.nix Normal file
View File

@ -0,0 +1,177 @@
# This file has been generated by Niv.
let
#
# The fetchers. fetch_<type> fetches specs of type <type>.
#
fetch_file = pkgs: name: spec:
let
name' = sanitizeName name + "-src";
in
if spec.builtin or true then
builtins_fetchurl { inherit (spec) url sha256; name = name'; }
else
pkgs.fetchurl { inherit (spec) url sha256; name = name'; };
fetch_tarball = pkgs: name: spec:
let
name' = sanitizeName name + "-src";
in
if spec.builtin or true then
builtins_fetchTarball { name = name'; inherit (spec) url sha256; }
else
pkgs.fetchzip { name = name'; inherit (spec) url sha256; };
fetch_git = name: spec:
let
ref =
if spec ? ref then spec.ref else
if spec ? branch then "refs/heads/${spec.branch}" else
if spec ? tag then "refs/tags/${spec.tag}" else
abort "In git source '${name}': Please specify `ref`, `tag` or `branch`!";
in
builtins.fetchGit { url = spec.repo; inherit (spec) rev; inherit ref; };
fetch_local = spec: spec.path;
fetch_builtin-tarball = name: throw
''[${name}] The niv type "builtin-tarball" is deprecated. You should instead use `builtin = true`.
$ niv modify ${name} -a type=tarball -a builtin=true'';
fetch_builtin-url = name: throw
''[${name}] The niv type "builtin-url" will soon be deprecated. You should instead use `builtin = true`.
$ niv modify ${name} -a type=file -a builtin=true'';
#
# Various helpers
#
# https://github.com/NixOS/nixpkgs/pull/83241/files#diff-c6f540a4f3bfa4b0e8b6bafd4cd54e8bR695
sanitizeName = name:
(
concatMapStrings (s: if builtins.isList s then "-" else s)
(
builtins.split "[^[:alnum:]+._?=-]+"
((x: builtins.elemAt (builtins.match "\\.*(.*)" x) 0) name)
)
);
# The set of packages used when specs are fetched using non-builtins.
mkPkgs = sources: system:
let
sourcesNixpkgs =
import (builtins_fetchTarball { inherit (sources.nixpkgs) url sha256; }) { inherit system; };
hasNixpkgsPath = builtins.any (x: x.prefix == "nixpkgs") builtins.nixPath;
hasThisAsNixpkgsPath = <nixpkgs> == ./.;
in
if builtins.hasAttr "nixpkgs" sources
then sourcesNixpkgs
else if hasNixpkgsPath && ! hasThisAsNixpkgsPath then
import <nixpkgs> { }
else
abort
''
Please specify either <nixpkgs> (through -I or NIX_PATH=nixpkgs=...) or
add a package called "nixpkgs" to your sources.json.
'';
# The actual fetching function.
fetch = pkgs: name: spec:
if ! builtins.hasAttr "type" spec then
abort "ERROR: niv spec ${name} does not have a 'type' attribute"
else if spec.type == "file" then fetch_file pkgs name spec
else if spec.type == "tarball" then fetch_tarball pkgs name spec
else if spec.type == "git" then fetch_git name spec
else if spec.type == "local" then fetch_local spec
else if spec.type == "builtin-tarball" then fetch_builtin-tarball name
else if spec.type == "builtin-url" then fetch_builtin-url name
else
abort "ERROR: niv spec ${name} has unknown type ${builtins.toJSON spec.type}";
# If the environment variable NIV_OVERRIDE_${name} is set, then use
# the path directly as opposed to the fetched source.
replace = name: drv:
let
saneName = stringAsChars (c: if isNull (builtins.match "[a-zA-Z0-9]" c) then "_" else c) name;
ersatz = builtins.getEnv "NIV_OVERRIDE_${saneName}";
in
if ersatz == "" then drv else
# this turns the string into an actual Nix path (for both absolute and
# relative paths)
if builtins.substring 0 1 ersatz == "/" then /. + ersatz else /. + builtins.getEnv "PWD" + "/${ersatz}";
# Ports of functions for older nix versions
# a Nix version of mapAttrs if the built-in doesn't exist
mapAttrs = builtins.mapAttrs or (
f: set: with builtins;
listToAttrs (map (attr: { name = attr; value = f attr set.${attr}; }) (attrNames set))
);
# https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/lists.nix#L295
range = first: last: if first > last then [ ] else builtins.genList (n: first + n) (last - first + 1);
# https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L257
stringToCharacters = s: map (p: builtins.substring p 1 s) (range 0 (builtins.stringLength s - 1));
# https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L269
stringAsChars = f: s: concatStrings (map f (stringToCharacters s));
concatMapStrings = f: list: concatStrings (map f list);
concatStrings = builtins.concatStringsSep "";
# https://github.com/NixOS/nixpkgs/blob/8a9f58a375c401b96da862d969f66429def1d118/lib/attrsets.nix#L331
optionalAttrs = cond: as: if cond then as else { };
# fetchTarball version that is compatible between all the versions of Nix
builtins_fetchTarball = { url, name ? null, sha256 }@attrs:
let
inherit (builtins) lessThan nixVersion fetchTarball;
in
if lessThan nixVersion "1.12" then
fetchTarball ({ inherit url; } // (optionalAttrs (!isNull name) { inherit name; }))
else
fetchTarball attrs;
# fetchurl version that is compatible between all the versions of Nix
builtins_fetchurl = { url, name ? null, sha256 }@attrs:
let
inherit (builtins) lessThan nixVersion fetchurl;
in
if lessThan nixVersion "1.12" then
fetchurl ({ inherit url; } // (optionalAttrs (!isNull name) { inherit name; }))
else
fetchurl attrs;
# Create the final "sources" from the config
mkSources = config:
mapAttrs
(
name: spec:
if builtins.hasAttr "outPath" spec
then
abort
"The values in sources.json should not have an 'outPath' attribute"
else
spec // { outPath = replace name (fetch config.pkgs name spec); }
)
config.sources;
# The "config" used by the fetchers
mkConfig =
{ sourcesFile ? if builtins.pathExists ./sources.json then ./sources.json else null
, sources ? if isNull sourcesFile then { } else builtins.fromJSON (builtins.readFile sourcesFile)
, system ? builtins.currentSystem
, pkgs ? mkPkgs sources system
}: rec {
# The sources, i.e. the attribute set of spec name to spec
inherit sources;
# The "pkgs" (evaluated nixpkgs) to use for e.g. non-builtin fetchers
inherit pkgs;
};
in
mkSources (mkConfig { }) // { __functor = _: settings: mkSources (mkConfig settings); }

13
shell.nix Normal file
View File

@ -0,0 +1,13 @@
let
sources = import ./nix/sources.nix;
pkgs = import ./nix/pkgs.nix { inherit sources; };
pre-commit = import ./nix/pre-commit.nix { inherit sources; };
in
pkgs.haskell.lib.buildStackProject {
name = "yamlparse-applicative-shell";
buildInputs = with pkgs; [
(import sources.niv { }).niv
zlib
] ++ pre-commit.tools;
shellHook = pre-commit.run.shellHook;
}

27
stack.yaml Normal file
View File

@ -0,0 +1,27 @@
resolver: nightly-2021-05-10
packages:
- autodocodec
- autodocodec-aeson
# - autodocodec-yaml
extra-deps:
- github: NorfairKing/validity
commit: 549f92de456dd7fbf21b84f2f6fdca9a3c6506d9
subdirs:
- validity
- genvalidity
- genvalidity-sydtest
- github: NorfairKing/safe-coloured-text
commit: b8333a8956975beab54d00fceaa835ee97c3347d
subdirs:
- safe-coloured-text
- safe-coloured-text-terminfo
- github: NorfairKing/sydtest
commit: 0125c6d6d17d44753c793adc8dcaa4284fef2b7d
subdirs:
- sydtest
- sydtest-discover
- envparse-0.4.1@sha256:989902e6368532548f61de1fa245ad2b39176cddd8743b20071af519a709ce30,2842
nix:
shell-file: shell.nix