Skip to content

Commit

Permalink
Merge pull request #61 from collective/update-ci
Browse files Browse the repository at this point in the history
Migrate CI from Travis-CI to GitHub actions
  • Loading branch information
datakurre committed May 14, 2021
2 parents e2b34b3 + 35f0420 commit 4fb9d47
Show file tree
Hide file tree
Showing 24 changed files with 2,810 additions and 182 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: "build"
on:
push:
pull_request:
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python: [python27, python39]
env:
COVERALLS_REPO_TOKEN: ${{ secrets.github_token }}
steps:
- uses: actions/checkout@v2.3.4
- uses: cachix/install-nix-action@v12
with:
nix_path: nixpkgs=channel:nixos-unstable
- uses: cachix/cachix-action@v8
with:
name: datakurre
- run: make nix-test nix-coverage nix-coveralls nix-docs PYTHON=${{ matrix.python }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
.coverage
.eggs/
.idea/
.pytest_cache/
bin/
docs/_build/
html/
Expand Down
11 changes: 0 additions & 11 deletions .travis.yml

This file was deleted.

36 changes: 28 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
PYTHON ?= python3
PYTHON ?= python39
ARGSTR ?= --argstr python $(PYTHON)

CACHIX_CACHE ?= datakurre

TEST = $(wildcard tests/*.py)
SRC = $(wildcard src/sphinxcontrib/httpexample/*.py)

Expand All @@ -9,25 +11,33 @@ all: test coverage

.PHONY: nix-%
nix-%: requirements.nix
nix-shell setup.nix $(ARGSTR) -A develop --run "$(MAKE) $*"
nix-shell setup.nix $(ARGSTR) -A package --run "$(MAKE) $*"

nix-env: requirements.nix
nix-build setup.nix $(ARGSTR) -A env

nix-shell: requirements.nix
nix-shell setup.nix $(ARGSTR) -A develop
nix-shell setup.nix $(ARGSTR) -A package

.PHONY: cache
cache:
nix-store --query --references $$(nix-instantiate shell.nix --argstr python $(PYTHON)) --references $$(nix-instantiate default.nix --argstr python $(PYTHON)) | \
xargs nix-store --realise | xargs nix-store --query --requisites | cachix push $(CACHIX_CACHE)

.PHONY: docs
docs: requirements.nix
nix-build release.nix $(ARGSTR) -A docs
sphinx-build -b html docs docs/html
ifeq "$(PYTHON)" "python27"
sphinx-build -b pdf docs docs/pdf
endif

.PHONY: coverage
coverage: .coverage
coverage report --fail-under=80

.PHONY: coveralls
coveralls: .coverage
coveralls
coveralls --service=github

.PHONY: test
test:
Expand All @@ -45,9 +55,19 @@ freeze:

###

.cache:
mkdir -p .cache
if [ -d ~/.cache/pip ]; then ln -s ~/.cache/pip ./.cache; fi

.coverage: $(TEST) $(SRC)
coverage run setup.py test

requirements.nix: requirements.txt
nix-shell setup.nix -A pip2nix \
--run "pip2nix generate -r requirements.txt --output=requirements.nix"
.PHONY: requirements
requirements: .cache requirements-$(PYTHON).nix

requirements-$(PYTHON).nix: .cache requirements-$(PYTHON).txt
pip2nix generate -r requirements-$(PYTHON).txt --output=requirements-$(PYTHON).nix

requirements-$(PYTHON).txt: .cache requirements.txt
pip2nix generate -r requirements.txt --output=requirements-$(PYTHON).nix
@grep "pname =\|version =" requirements-$(PYTHON).nix|awk "ORS=NR%2?FS:RS"|sed 's|.*"\(.*\)";.*version = "\(.*\)".*|\1==\2|' > requirements-$(PYTHON).txt
6 changes: 6 additions & 0 deletions default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{ pkgs ? import ./nix { nixpkgs = sources."nixpkgs"; }
, sources ? import ./nix/sources.nix {}
, python ? "python39"
}:

(import ./setup.nix { inherit pkgs sources python; }).package
24 changes: 24 additions & 0 deletions nix/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{ nixpkgs ? sources.nixpkgs
, config ? {
permittedInsecurePackages = [
"python2.7-pillow-6.2.2"
];
}
, sources ? import ./sources.nix
}:

let

overlay = _: pkgs: {
# pip2nix branches require specific nixpkgs branch (for pip)
pip2nix = ((import (sources."pip2nix" + "/release.nix") {
pkgs = import sources."nixpkgs-20.09" {};
}).pip2nix);
};

pkgs = import nixpkgs {
overlays = [ overlay ];
inherit config;
};

in pkgs
50 changes: 50 additions & 0 deletions nix/sources.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"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": "master",
"description": "Nix Packages collection",
"homepage": "",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "580d6f695192038972087caa8a7368ffcc61d98e",
"sha256": "117cgi7gq5mfpg8csy0wfrcyndsnrcfwpxwlm70710v091jmxpa6",
"type": "tarball",
"url": "https://github.com/NixOS/nixpkgs/archive/580d6f695192038972087caa8a7368ffcc61d98e.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
},
"nixpkgs-20.09": {
"branch": "release-20.09",
"description": "Nix Packages collection",
"homepage": "",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "7cdef6071a655c927675ab586979e6fe676a934d",
"sha256": "059yrvi2pw1blmy0qpgw48y6djwc28r7qhancdr3xb74f89ai9fm",
"type": "tarball",
"url": "https://github.com/NixOS/nixpkgs/archive/7cdef6071a655c927675ab586979e6fe676a934d.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
},
"pip2nix": {
"branch": "master",
"description": "Freeze pip-installable packages into Nix expressions [maintainer=@datakurre]",
"homepage": "",
"owner": "nix-community",
"repo": "pip2nix",
"rev": "5f58838d9c7b0e05cb0744cdbcb11d15f48066fd",
"sha256": "0lq0mrvfvpbnikkxwmlqk0p72ki2160r417y2nfpb652ng77daqr",
"type": "tarball",
"url": "https://github.com/nix-community/pip2nix/archive/5f58838d9c7b0e05cb0744cdbcb11d15f48066fd.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
}
}
148 changes: 148 additions & 0 deletions nix/sources.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
# This file has been generated by Niv.

let

#
# The fetchers. fetch_<type> fetches specs of type <type>.
#

fetch_file = pkgs: spec:
if spec.builtin or true then
builtins_fetchurl { inherit (spec) url sha256; }
else
pkgs.fetchurl { inherit (spec) url sha256; };

fetch_tarball = pkgs: name: spec:
let
ok = str: ! builtins.isNull (builtins.match "[a-zA-Z0-9+-._?=]" str);
# sanitize the name, though nix will still fail if name starts with period
name' = stringAsChars (x: if ! ok x then "-" else x) "${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 = spec:
builtins.fetchGit { url = spec.repo; inherit (spec) rev 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
#

# The set of packages used when specs are fetched using non-builtins.
mkPkgs = sources:
let
sourcesNixpkgs =
import (builtins_fetchTarball { inherit (sources.nixpkgs) url sha256; }) {};
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 spec
else if spec.type == "tarball" then fetch_tarball pkgs name spec
else if spec.type == "git" then fetch_git 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 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));
concatStrings = builtins.concatStringsSep "";

# fetchTarball version that is compatible between all the versions of Nix
builtins_fetchTarball = { url, name, sha256 }@attrs:
let
inherit (builtins) lessThan nixVersion fetchTarball;
in
if lessThan nixVersion "1.12" then
fetchTarball { inherit name url; }
else
fetchTarball attrs;

# fetchurl version that is compatible between all the versions of Nix
builtins_fetchurl = { url, sha256 }@attrs:
let
inherit (builtins) lessThan nixVersion fetchurl;
in
if lessThan nixVersion "1.12" then
fetchurl { inherit url; }
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)
, pkgs ? mkPkgs sources
}: 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); }
45 changes: 0 additions & 45 deletions release.nix

This file was deleted.

0 comments on commit 4fb9d47

Please sign in to comment.