Skip to content

Commit e784c73

Browse files
committed
devshell cli: add source filter
1 parent b847291 commit e784c73

2 files changed

Lines changed: 68 additions & 3 deletions

File tree

go/devshell/default.nix

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
1-
{ lib, buildGoModule }:
1+
{ buildGoModule }:
2+
let
3+
source = import ../../nix/source.nix;
4+
in
25
buildGoModule {
36
name = "devshell";
4-
src = lib.cleanSource ./.;
5-
vendorSha256 = "sha256-CksQhzcKuHWihdJeTazQ5EG8aFkZ5cpss90eRCT+ERc=";
7+
src = source.filter {
8+
path = ./.;
9+
allow = [
10+
./go.mod
11+
./go.sum
12+
(source.matchExt "go")
13+
];
14+
};
15+
vendorSha256 = "sha256-NFsQoPDXEeOmyLR2bCgKuRHw2sjhGQbUmHV8bMJzANw=";
616
}

nix/source.nix

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# A standalone source filtering library
2+
let
3+
inherit (builtins)
4+
any
5+
isFunction
6+
isString
7+
isPath
8+
map
9+
stringLength
10+
substring
11+
;
12+
13+
# Copied from the nixpkgs stdlib
14+
hasSuffix =
15+
# Suffix to check for
16+
suffix:
17+
# Input string
18+
content:
19+
let
20+
lenContent = stringLength content;
21+
lenSuffix = stringLength suffix;
22+
in
23+
lenContent >= lenSuffix &&
24+
substring (lenContent - lenSuffix) lenContent content == suffix;
25+
26+
# If an argument to allow or deny is a path, transform it to a matcher.
27+
#
28+
# This probably needs more work, I don't think that it works on sub-folders.
29+
toMatcher = f:
30+
let
31+
path_ = toString f;
32+
in
33+
if isFunction f then f
34+
else
35+
(path: type: path_ == toString path);
36+
in
37+
{
38+
# Match paths with the given extension
39+
matchExt = ext:
40+
path: type:
41+
(hasSuffix ".${ext}" path);
42+
43+
# A proper filter
44+
filter = { path, name ? "source", allow ? [ ], deny ? [ ] }:
45+
let
46+
allow_ = builtins.map toMatcher allow;
47+
deny_ = builtins.map toMatcher deny;
48+
in
49+
builtins.path {
50+
inherit name path;
51+
filter = path: type:
52+
(builtins.any (f: f path type) allow_) &&
53+
(!builtins.any (f: f path type) deny_);
54+
};
55+
}

0 commit comments

Comments
 (0)