···60606161### Building it yourself
62626363-This project uses [Nix](https://nixos.org/features.html) to manage the project's environment. If you'd like to build this project without Nix, check out the dependencies in the `shell.nix` file (most are available through Homebrew as well).
6363+This project uses [Nix](https://nixos.org/features.html) to manage the project's environment. If you'd like to build this project without Nix, check out the dependencies in the `nix/shell.nix` file (most are available through Homebrew as well).
646465656666```shell
···11-# This file has been generated by Niv.
22-33-let
44-55- #
66- # The fetchers. fetch_<type> fetches specs of type <type>.
77- #
88-99- fetch_file = pkgs: name: spec:
1010- let
1111- name' = sanitizeName name + "-src";
1212- in
1313- if spec.builtin or true then
1414- builtins_fetchurl { inherit (spec) url sha256; name = name'; }
1515- else
1616- pkgs.fetchurl { inherit (spec) url sha256; name = name'; };
1717-1818- fetch_tarball = pkgs: name: spec:
1919- let
2020- name' = sanitizeName name + "-src";
2121- in
2222- if spec.builtin or true then
2323- builtins_fetchTarball { name = name'; inherit (spec) url sha256; }
2424- else
2525- pkgs.fetchzip { name = name'; inherit (spec) url sha256; };
2626-2727- fetch_git = name: spec:
2828- let
2929- ref =
3030- if spec ? ref then spec.ref else
3131- if spec ? branch then "refs/heads/${spec.branch}" else
3232- if spec ? tag then "refs/tags/${spec.tag}" else
3333- abort "In git source '${name}': Please specify `ref`, `tag` or `branch`!";
3434- in
3535- builtins.fetchGit { url = spec.repo; inherit (spec) rev; inherit ref; };
3636-3737- fetch_local = spec: spec.path;
3838-3939- fetch_builtin-tarball = name: throw
4040- ''[${name}] The niv type "builtin-tarball" is deprecated. You should instead use `builtin = true`.
4141- $ niv modify ${name} -a type=tarball -a builtin=true'';
4242-4343- fetch_builtin-url = name: throw
4444- ''[${name}] The niv type "builtin-url" will soon be deprecated. You should instead use `builtin = true`.
4545- $ niv modify ${name} -a type=file -a builtin=true'';
4646-4747- #
4848- # Various helpers
4949- #
5050-5151- # https://github.com/NixOS/nixpkgs/pull/83241/files#diff-c6f540a4f3bfa4b0e8b6bafd4cd54e8bR695
5252- sanitizeName = name:
5353- (
5454- concatMapStrings (s: if builtins.isList s then "-" else s)
5555- (
5656- builtins.split "[^[:alnum:]+._?=-]+"
5757- ((x: builtins.elemAt (builtins.match "\\.*(.*)" x) 0) name)
5858- )
5959- );
6060-6161- # The set of packages used when specs are fetched using non-builtins.
6262- mkPkgs = sources: system:
6363- let
6464- sourcesNixpkgs =
6565- import (builtins_fetchTarball { inherit (sources.nixpkgs) url sha256; }) { inherit system; };
6666- hasNixpkgsPath = builtins.any (x: x.prefix == "nixpkgs") builtins.nixPath;
6767- hasThisAsNixpkgsPath = <nixpkgs> == ./.;
6868- in
6969- if builtins.hasAttr "nixpkgs" sources
7070- then sourcesNixpkgs
7171- else if hasNixpkgsPath && ! hasThisAsNixpkgsPath then
7272- import <nixpkgs> {}
7373- else
7474- abort
7575- ''
7676- Please specify either <nixpkgs> (through -I or NIX_PATH=nixpkgs=...) or
7777- add a package called "nixpkgs" to your sources.json.
7878- '';
7979-8080- # The actual fetching function.
8181- fetch = pkgs: name: spec:
8282-8383- if ! builtins.hasAttr "type" spec then
8484- abort "ERROR: niv spec ${name} does not have a 'type' attribute"
8585- else if spec.type == "file" then fetch_file pkgs name spec
8686- else if spec.type == "tarball" then fetch_tarball pkgs name spec
8787- else if spec.type == "git" then fetch_git name spec
8888- else if spec.type == "local" then fetch_local spec
8989- else if spec.type == "builtin-tarball" then fetch_builtin-tarball name
9090- else if spec.type == "builtin-url" then fetch_builtin-url name
9191- else
9292- abort "ERROR: niv spec ${name} has unknown type ${builtins.toJSON spec.type}";
9393-9494- # If the environment variable NIV_OVERRIDE_${name} is set, then use
9595- # the path directly as opposed to the fetched source.
9696- replace = name: drv:
9797- let
9898- saneName = stringAsChars (c: if isNull (builtins.match "[a-zA-Z0-9]" c) then "_" else c) name;
9999- ersatz = builtins.getEnv "NIV_OVERRIDE_${saneName}";
100100- in
101101- if ersatz == "" then drv else
102102- # this turns the string into an actual Nix path (for both absolute and
103103- # relative paths)
104104- if builtins.substring 0 1 ersatz == "/" then /. + ersatz else /. + builtins.getEnv "PWD" + "/${ersatz}";
105105-106106- # Ports of functions for older nix versions
107107-108108- # a Nix version of mapAttrs if the built-in doesn't exist
109109- mapAttrs = builtins.mapAttrs or (
110110- f: set: with builtins;
111111- listToAttrs (map (attr: { name = attr; value = f attr set.${attr}; }) (attrNames set))
112112- );
113113-114114- # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/lists.nix#L295
115115- range = first: last: if first > last then [] else builtins.genList (n: first + n) (last - first + 1);
116116-117117- # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L257
118118- stringToCharacters = s: map (p: builtins.substring p 1 s) (range 0 (builtins.stringLength s - 1));
119119-120120- # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L269
121121- stringAsChars = f: s: concatStrings (map f (stringToCharacters s));
122122- concatMapStrings = f: list: concatStrings (map f list);
123123- concatStrings = builtins.concatStringsSep "";
124124-125125- # https://github.com/NixOS/nixpkgs/blob/8a9f58a375c401b96da862d969f66429def1d118/lib/attrsets.nix#L331
126126- optionalAttrs = cond: as: if cond then as else {};
127127-128128- # fetchTarball version that is compatible between all the versions of Nix
129129- builtins_fetchTarball = { url, name ? null, sha256 }@attrs:
130130- let
131131- inherit (builtins) lessThan nixVersion fetchTarball;
132132- in
133133- if lessThan nixVersion "1.12" then
134134- fetchTarball ({ inherit url; } // (optionalAttrs (!isNull name) { inherit name; }))
135135- else
136136- fetchTarball attrs;
137137-138138- # fetchurl version that is compatible between all the versions of Nix
139139- builtins_fetchurl = { url, name ? null, sha256 }@attrs:
140140- let
141141- inherit (builtins) lessThan nixVersion fetchurl;
142142- in
143143- if lessThan nixVersion "1.12" then
144144- fetchurl ({ inherit url; } // (optionalAttrs (!isNull name) { inherit name; }))
145145- else
146146- fetchurl attrs;
147147-148148- # Create the final "sources" from the config
149149- mkSources = config:
150150- mapAttrs (
151151- name: spec:
152152- if builtins.hasAttr "outPath" spec
153153- then abort
154154- "The values in sources.json should not have an 'outPath' attribute"
155155- else
156156- spec // { outPath = replace name (fetch config.pkgs name spec); }
157157- ) config.sources;
158158-159159- # The "config" used by the fetchers
160160- mkConfig =
161161- { sourcesFile ? if builtins.pathExists ./sources.json then ./sources.json else null
162162- , sources ? if isNull sourcesFile then {} else builtins.fromJSON (builtins.readFile sourcesFile)
163163- , system ? builtins.currentSystem
164164- , pkgs ? mkPkgs sources system
165165- }: rec {
166166- # The sources, i.e. the attribute set of spec name to spec
167167- inherit sources;
168168-169169- # The "pkgs" (evaluated nixpkgs) to use for e.g. non-builtin fetchers
170170- inherit pkgs;
171171- };
172172-173173-in
174174-mkSources (mkConfig {}) // { __functor = _: settings: mkSources (mkConfig settings); }