Skip to content

Commit

Permalink
Merge pull request #99: Add loal development override option
Browse files Browse the repository at this point in the history
  • Loading branch information
piegamesde authored Sep 26, 2024
2 parents 36f49ba + 8ece9ce commit 9ba93b1
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 4 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,15 @@ Parameter example:
$ npins add gitlab my-org my-private-repo --token H_BRqzV3NcaPvXcYs2Xf
```

### Using local sources during development

While npins allows you to pin dependencies in reproducible fashion, it is often desirable to allow fast impure iterations during development.
Npins supports local overrides for this.
If your `sources.json` contains a source named `abc`, you can e.g. develop from `/abc` by exposing the environment variable `NPINS_OVERRIDE_abc=/abc`.
Please note, that only alphanumerical characters and _ are allow characters in overriden sources.
All other characters are converted to _.
Also check, that you are building impure, if you are wondering, why these overrides are maybe not becoming active.

## Contributing

Contributions to this project are welcome in the form of GitHub Issues or PRs. Please consider the following before creating PRs:
Expand Down
9 changes: 9 additions & 0 deletions README.md.in
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,15 @@ Parameter example:
$ npins add gitlab my-org my-private-repo --token H_BRqzV3NcaPvXcYs2Xf
```

### Using local sources during development

While npins allows you to pin dependencies in reproducible fashion, it is often desirable to allow fast impure iterations during development.
Npins supports local overrides for this.
If your `sources.json` contains a source named `abc`, you can e.g. develop from `/abc` by exposing the environment variable `NPINS_OVERRIDE_abc=/abc`.
Please note, that only alphanumerical characters and _ are allow characters in overriden sources.
All other characters are converted to _.
Also check, that you are building impure, if you are wondering, why these overrides are maybe not becoming active.

## Contributing

Contributions to this project are welcome in the form of GitHub Issues or PRs. Please consider the following before creating PRs:
Expand Down
40 changes: 37 additions & 3 deletions src/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,42 @@ let
data = builtins.fromJSON (builtins.readFile ./sources.json);
version = data.version;

# 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 "";

# If the environment variable NPINS_OVERRIDE_${name} is set, then use
# the path directly as opposed to the fetched source.
# (Taken from Niv for compatibility)
mayOverride =
name: path:
let
envVarName = "NPINS_OVERRIDE_${saneName}";
saneName = stringAsChars (c: if (builtins.match "[a-zA-Z0-9]" c) == null then "_" else c) name;
ersatz = builtins.getEnv envVarName;
in
if ersatz == "" then
path
else
# this turns the string into an actual Nix path (for both absolute and
# relative paths)
builtins.trace "Overriding path of \"${name}\" with \"${ersatz}\" due to set \"${envVarName}\"" (
if builtins.substring 0 1 ersatz == "/" then
/. + ersatz
else
/. + builtins.getEnv "PWD" + "/${ersatz}"
);

mkSource =
spec:
name: spec:
assert spec ? type;
let
path =
Expand All @@ -19,7 +53,7 @@ let
else
builtins.throw "Unknown source type ${spec.type}";
in
spec // { outPath = path; };
spec // { outPath = mayOverride name path; };

mkGitSource =
{
Expand Down Expand Up @@ -84,6 +118,6 @@ let
};
in
if version == 4 then
builtins.mapAttrs (_: mkSource) data.pins
builtins.mapAttrs mkSource data.pins
else
throw "Unsupported format version ${toString version} in sources.json. Try running `npins upgrade`"
26 changes: 25 additions & 1 deletion test.nix
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ let
if [[ "$a" = "$b" ]]; then echo " OK"; else echo " FAIL"; exit 1; fi
}
function neq() {
local a=$1
local b=$2
printf '[[ "%s" != "%s" ]]' "$a" "$b"
if [[ "$a" != "$b" ]]; then echo " OK"; else echo " FAIL"; exit 1; fi
}
function resolveGitCommit() {
local repo=$1
local commitish=''${2:-main}
Expand All @@ -28,7 +35,8 @@ let
'';

inherit (pkgs) lib;
# Generate a git repository hat can be served via HTTP.

# Generate a git repository that can be served via HTTP.
#
# By default the repository will contain an empty `test.txt`
# file. For all defined tags the name of the tag is written to that
Expand Down Expand Up @@ -555,4 +563,20 @@ in
'';
};
};

gitDependencyOverride = mkGitTest rec {
name = "git-dependency-override";
repositories."foo" = gitRepo;
commands = ''
npins init --bare
npins add git http://localhost:8000/foo -b test-branch
npins show
OUTPATH=$(NPINS_OVERRIDE_foo=/foo_overriden nix-instantiate --eval npins -A foo.outPath --impure)
eq "$OUTPATH" "/foo_overriden"
OUTPATH=$(nix-instantiate --eval npins -A foo.outPath)
neq "$OUTPATH" "/foo_overriden"
'';
};
}

0 comments on commit 9ba93b1

Please sign in to comment.