~jack/libtelnet-haskell

8c00d3685dc90a77a31a55da315640cfe5cb32b0 — Jack Kelly 6 months ago 5cf6477
Update CI, use flakes
9 files changed, 136 insertions(+), 274 deletions(-)

M .build.yml
M default.nix
A flake.lock
A flake.nix
M libtelnet.cabal
D mkdocs
D nix/sources.json
D nix/sources.nix
M shell.nix
M .build.yml => .build.yml +9 -11
@@ 1,21 1,19 @@
image: nixos/latest
repositories:
  nixpkgs: https://nixos.org/channels/nixpkgs-unstable
image: nixos/unstable
sources:
  - https://git.sr.ht/~jack/libtelnet-haskell
tasks:
  - ghc901: |
  - ghc922: |
      cd libtelnet-haskell
      nix-build --arg buildExample true --argstr compiler ghc901
  - ghc8106: |
      nix build --extra-experimental-features 'nix-command flakes' -L .#ghc922
  - ghc902: |
      cd libtelnet-haskell
      nix-build --arg buildExample true --argstr compiler ghc8106
  - ghc884: |
      nix build --extra-experimental-features 'nix-command flakes' -L .#ghc902
  - ghc8107: |
      cd libtelnet-haskell
      nix-build --arg buildExample true --argstr compiler ghc884
  - ghc865: |
      nix build --extra-experimental-features 'nix-command flakes' -L .#ghc8107
  - ghc884: |
      cd libtelnet-haskell
      nix-build --arg buildExample true --argstr compiler ghc865Binary
      nix build --extra-experimental-features 'nix-command flakes' -L .#ghc884
triggers:
  - condition: failure
    action: email

M default.nix => default.nix +9 -35
@@ 1,35 1,9 @@
{ sources ? import ./nix/sources.nix
, nixpkgsArgs ? {}
, nixpkgs ? import sources.nixpkgs nixpkgsArgs
, compiler ? "default"
, doBenchmark ? false
, buildExample ? false
}:

let
  inherit (nixpkgs) pkgs;

  haskellPackages =
    if compiler == "default"
    then pkgs.haskellPackages
    else pkgs.haskell.packages.${compiler};

  benchmarkVariant =
    if doBenchmark
    then pkgs.haskell.lib.doBenchmark
    else pkgs.lib.id;

  # Example code's build-depends are behind a flag, so cabal2nix
  # doesn't add them automatically.
  exampleVariant = pkg:
    if buildExample
    then with pkgs.haskell.lib; enableCabalFlag
      (addBuildDepends pkg (with haskellPackages; [monad-loops network-simple]))
      "example"
    else pkg;

  libtelnet = haskellPackages.callPackage ./libtelnet.nix {
    inherit (pkgs) libtelnet;
  };
in
  exampleVariant (benchmarkVariant libtelnet)
(import (
  let
    lock = builtins.fromJSON (builtins.readFile ./flake.lock);
  in fetchTarball {
    url = "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz";
    sha256 = lock.nodes.flake-compat.locked.narHash; }
) {
    src =  ./.;
}).defaultNix

A flake.lock => flake.lock +59 -0
@@ 0,0 1,59 @@
{
  "nodes": {
    "flake-compat": {
      "flake": false,
      "locked": {
        "lastModified": 1650374568,
        "narHash": "sha256-Z+s0J8/r907g149rllvwhb4pKi8Wam5ij0st8PwAh+E=",
        "owner": "edolstra",
        "repo": "flake-compat",
        "rev": "b4a34015c698c7793d592d66adbab377907a2be8",
        "type": "github"
      },
      "original": {
        "owner": "edolstra",
        "repo": "flake-compat",
        "type": "github"
      }
    },
    "flake-utils": {
      "locked": {
        "lastModified": 1652776076,
        "narHash": "sha256-gzTw/v1vj4dOVbpBSJX4J0DwUR6LIyXo7/SuuTJp1kM=",
        "owner": "numtide",
        "repo": "flake-utils",
        "rev": "04c1b180862888302ddfb2e3ad9eaa63afc60cf8",
        "type": "github"
      },
      "original": {
        "owner": "numtide",
        "repo": "flake-utils",
        "type": "github"
      }
    },
    "nixpkgs": {
      "locked": {
        "lastModified": 1653104397,
        "narHash": "sha256-KEEXl264n/X/UnFW+kBL37BXanXTgyVPCGZ8XxwiGrU=",
        "owner": "nixos",
        "repo": "nixpkgs",
        "rev": "bd275feff751071bbfd4d9efb297a46855cd2ea6",
        "type": "github"
      },
      "original": {
        "owner": "nixos",
        "repo": "nixpkgs",
        "type": "github"
      }
    },
    "root": {
      "inputs": {
        "flake-compat": "flake-compat",
        "flake-utils": "flake-utils",
        "nixpkgs": "nixpkgs"
      }
    }
  },
  "root": "root",
  "version": 7
}

A flake.nix => flake.nix +43 -0
@@ 0,0 1,43 @@
{
  description = "reflex-backend-socket";

  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs";
    flake-compat = {
      url = "github:edolstra/flake-compat";
      flake = false;
    };
    flake-utils.url = "github:numtide/flake-utils";
  };

  outputs = inputs: inputs.flake-utils.lib.eachDefaultSystem (system:
    let
      nixpkgs = import inputs.nixpkgs { inherit system; };
      inherit (nixpkgs.haskell.packages) ghc884 ghc8107 ghc902 ghc922;

      # Because the package is called libtelnet, we have to pass in
      # its pkgconfig dependency explicitly. Also turn on the example.
      makeLibtelnet = haskellPackages:
        let
          pkg = haskellPackages.callPackage ./libtelnet.nix  {
            inherit (nixpkgs) libtelnet;
          };
        in with nixpkgs.haskell.lib;
          enableCabalFlag
          (addBuildDepends pkg
            (with haskellPackages; [monad-loops network-simple]))
          "example";
    in rec
    {
      defaultPackage = packages.ghc922;
      packages = {
        ghc884 = makeLibtelnet ghc884;
        ghc8107 = makeLibtelnet ghc8107;
        ghc902 = makeLibtelnet ghc902;
        ghc922 = makeLibtelnet ghc922;
      };
      devShell = devShells.ghc922;
      devShells = builtins.mapAttrs (_: v: v.env) packages;
    }
  );
}

M libtelnet.cabal => libtelnet.cabal +3 -3
@@ 16,10 16,10 @@ copyright:           (c) 2017-2021 Jack Kelly
category:            Network
build-type:          Simple
extra-source-files:  COPYING, CHANGELOG.md, README.md
tested-with:         GHC == 9.0.1
                   , GHC == 8.10.6
tested-with:         GHC == 9.2.2
                   , GHC == 9.0.2
                   , GHC == 8.10.7
                   , GHC == 8.8.4
                   , GHC == 8.6.5

flag example
  description:         Build the example program

D mkdocs => mkdocs +0 -9
@@ 1,9 0,0 @@
#!/bin/sh
set -e

dir=$(mktemp -d dist-docs.XXXXXX)
trap 'rm -fr "$dir"' EXIT

cabal haddock --builddir="$dir" --for-hackage --haddock-option=--hyperlinked-source
# Starting with cabal 2.0, `--publish` is needed for uploading to non-candidate releases
cabal upload -d $dir/*-docs.tar.gz

D nix/sources.json => nix/sources.json +0 -26
@@ 1,26 0,0 @@
{
    "niv": {
        "branch": "master",
        "description": "Easy dependency management for Nix projects",
        "homepage": "https://github.com/nmattia/niv",
        "owner": "nmattia",
        "repo": "niv",
        "rev": "e0ca65c81a2d7a4d82a189f1e23a48d59ad42070",
        "sha256": "1pq9nh1d8nn3xvbdny8fafzw87mj7gsmp6pxkdl65w2g18rmcmzx",
        "type": "tarball",
        "url": "https://github.com/nmattia/niv/archive/e0ca65c81a2d7a4d82a189f1e23a48d59ad42070.tar.gz",
        "url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
    },
    "nixpkgs": {
        "branch": "master",
        "description": "Nix Packages collection",
        "homepage": "",
        "owner": "NixOS",
        "repo": "nixpkgs",
        "rev": "098ca1a9aae33696adc149828e6722598be2d47c",
        "sha256": "161hmld0mdkhlpmrwqyfa6r0x6hjz98cyqp665400262lldlcw11",
        "type": "tarball",
        "url": "https://github.com/NixOS/nixpkgs/archive/098ca1a9aae33696adc149828e6722598be2d47c.tar.gz",
        "url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
    }
}

D nix/sources.nix => nix/sources.nix +0 -174
@@ 1,174 0,0 @@
# 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); }

M shell.nix => shell.nix +13 -16
@@ 1,16 1,13 @@
{ sources ? import ./nix/sources.nix
, nixpkgsArgs ? {}
, nixpkgs ? import sources.nixpkgs nixpkgsArgs
, compiler ? "default"
, doBenchmark ? false
}:
let
  inherit (nixpkgs) pkgs;
  env = (import ./default.nix { inherit nixpkgs compiler doBenchmark; }).env;
  niv = (import sources.niv {}).niv;
in
  env.overrideAttrs (oldAttrs: {
    buildInputs = with pkgs.haskellPackages; oldAttrs.buildInputs ++ [
      cabal-install ghcid niv
    ];
  })
(import
  (
    let
      lock = builtins.fromJSON (builtins.readFile ./flake.lock);
    in
    fetchTarball {
      url = "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz";
      sha256 = lock.nodes.flake-compat.locked.narHash;
    }
  )
  {
    src = ./.;
  }).shellNix