From 9a7cf7e1ddba75e66dcb7a2626f38ba4a1f88936 Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Wed, 11 Aug 2021 11:06:50 -0400 Subject: [PATCH] initial commit Signed-off-by: Christine Dodrill --- .envrc | 1 + LICENSE | 122 +++++++++++++++++++++++++- common/default.nix | 39 +++++++++ common/paranoid.nix | 64 ++++++++++++++ common/users.nix | 50 +++++++++++ images/build.nix | 9 ++ images/configuration.nix | 8 ++ images/make-image.nix | 182 +++++++++++++++++++++++++++++++++++++++ images/result | 1 + nix/sources.json | 38 ++++++++ nix/sources.nix | 174 +++++++++++++++++++++++++++++++++++++ shell.nix | 10 +++ 12 files changed, 695 insertions(+), 3 deletions(-) create mode 100644 .envrc create mode 100644 common/default.nix create mode 100644 common/paranoid.nix create mode 100644 common/users.nix create mode 100644 images/build.nix create mode 100644 images/configuration.nix create mode 100644 images/make-image.nix create mode 120000 images/result create mode 100644 nix/sources.json create mode 100644 nix/sources.nix create mode 100644 shell.nix diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..051d09d --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +eval "$(lorri direnv)" diff --git a/LICENSE b/LICENSE index 72c7baf..1625c17 100644 --- a/LICENSE +++ b/LICENSE @@ -1,5 +1,121 @@ -Copyright (C) 2006 by Rob Landley +Creative Commons Legal Code -Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. +CC0 1.0 Universal -THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE + LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN + ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS + INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES + REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS + PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM + THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED + HEREUNDER. + +Statement of Purpose + +The laws of most jurisdictions throughout the world automatically confer +exclusive Copyright and Related Rights (defined below) upon the creator +and subsequent owner(s) (each and all, an "owner") of an original work of +authorship and/or a database (each, a "Work"). + +Certain owners wish to permanently relinquish those rights to a Work for +the purpose of contributing to a commons of creative, cultural and +scientific works ("Commons") that the public can reliably and without fear +of later claims of infringement build upon, modify, incorporate in other +works, reuse and redistribute as freely as possible in any form whatsoever +and for any purposes, including without limitation commercial purposes. +These owners may contribute to the Commons to promote the ideal of a free +culture and the further production of creative, cultural and scientific +works, or to gain reputation or greater distribution for their Work in +part through the use and efforts of others. + +For these and/or other purposes and motivations, and without any +expectation of additional consideration or compensation, the person +associating CC0 with a Work (the "Affirmer"), to the extent that he or she +is an owner of Copyright and Related Rights in the Work, voluntarily +elects to apply CC0 to the Work and publicly distribute the Work under its +terms, with knowledge of his or her Copyright and Related Rights in the +Work and the meaning and intended legal effect of CC0 on those rights. + +1. Copyright and Related Rights. A Work made available under CC0 may be +protected by copyright and related or neighboring rights ("Copyright and +Related Rights"). Copyright and Related Rights include, but are not +limited to, the following: + + i. the right to reproduce, adapt, distribute, perform, display, + communicate, and translate a Work; + ii. moral rights retained by the original author(s) and/or performer(s); +iii. publicity and privacy rights pertaining to a person's image or + likeness depicted in a Work; + iv. rights protecting against unfair competition in regards to a Work, + subject to the limitations in paragraph 4(a), below; + v. rights protecting the extraction, dissemination, use and reuse of data + in a Work; + vi. database rights (such as those arising under Directive 96/9/EC of the + European Parliament and of the Council of 11 March 1996 on the legal + protection of databases, and under any national implementation + thereof, including any amended or successor version of such + directive); and +vii. other similar, equivalent or corresponding rights throughout the + world based on applicable law or treaty, and any national + implementations thereof. + +2. Waiver. To the greatest extent permitted by, but not in contravention +of, applicable law, Affirmer hereby overtly, fully, permanently, +irrevocably and unconditionally waives, abandons, and surrenders all of +Affirmer's Copyright and Related Rights and associated claims and causes +of action, whether now known or unknown (including existing as well as +future claims and causes of action), in the Work (i) in all territories +worldwide, (ii) for the maximum duration provided by applicable law or +treaty (including future time extensions), (iii) in any current or future +medium and for any number of copies, and (iv) for any purpose whatsoever, +including without limitation commercial, advertising or promotional +purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each +member of the public at large and to the detriment of Affirmer's heirs and +successors, fully intending that such Waiver shall not be subject to +revocation, rescission, cancellation, termination, or any other legal or +equitable action to disrupt the quiet enjoyment of the Work by the public +as contemplated by Affirmer's express Statement of Purpose. + +3. Public License Fallback. Should any part of the Waiver for any reason +be judged legally invalid or ineffective under applicable law, then the +Waiver shall be preserved to the maximum extent permitted taking into +account Affirmer's express Statement of Purpose. In addition, to the +extent the Waiver is so judged Affirmer hereby grants to each affected +person a royalty-free, non transferable, non sublicensable, non exclusive, +irrevocable and unconditional license to exercise Affirmer's Copyright and +Related Rights in the Work (i) in all territories worldwide, (ii) for the +maximum duration provided by applicable law or treaty (including future +time extensions), (iii) in any current or future medium and for any number +of copies, and (iv) for any purpose whatsoever, including without +limitation commercial, advertising or promotional purposes (the +"License"). The License shall be deemed effective as of the date CC0 was +applied by Affirmer to the Work. Should any part of the License for any +reason be judged legally invalid or ineffective under applicable law, such +partial invalidity or ineffectiveness shall not invalidate the remainder +of the License, and in such case Affirmer hereby affirms that he or she +will not (i) exercise any of his or her remaining Copyright and Related +Rights in the Work or (ii) assert any associated claims and causes of +action with respect to the Work, in either case contrary to Affirmer's +express Statement of Purpose. + +4. Limitations and Disclaimers. + + a. No trademark or patent rights held by Affirmer are waived, abandoned, + surrendered, licensed or otherwise affected by this document. + b. Affirmer offers the Work as-is and makes no representations or + warranties of any kind concerning the Work, express, implied, + statutory or otherwise, including without limitation warranties of + title, merchantability, fitness for a particular purpose, non + infringement, or the absence of latent or other defects, accuracy, or + the present or absence of errors, whether or not discoverable, all to + the greatest extent permissible under applicable law. + c. Affirmer disclaims responsibility for clearing rights of other persons + that may apply to the Work or any use thereof, including without + limitation any person's Copyright and Related Rights in the Work. + Further, Affirmer disclaims responsibility for obtaining any necessary + consents, permissions or other rights required for any use of the + Work. + d. Affirmer understands and acknowledges that Creative Commons is not a + party to this document and has no duty or obligation with respect to + this CC0 or use of the Work. \ No newline at end of file diff --git a/common/default.nix b/common/default.nix new file mode 100644 index 0000000..462414e --- /dev/null +++ b/common/default.nix @@ -0,0 +1,39 @@ +{ config, lib, pkgs, ... }: + +{ + imports = [ ./paranoid.nix ./users.nix ]; + + nix.autoOptimiseStore = true; + + users.users.root.openssh.authorizedKeys.keys = [ + "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPg9gYKVglnO2HQodSJt4z4mNrUSUiyJQ7b+J798bwD9" + ]; + + services.tailscale.enable = true; + + # Tell the firewall to implicitly trust packets routed over Tailscale: + networking.firewall.trustedInterfaces = [ "tailscale0" ]; + + security.auditd.enable = true; + security.audit.enable = true; + security.audit.rules = [ "-a exit,always -F arch=b64 -S execve" ]; + + security.sudo.execWheelOnly = true; + environment.defaultPackages = lib.mkForce [ ]; + + services.openssh = { + passwordAuthentication = false; + allowSFTP = false; # Don't set this if you need sftp + challengeResponseAuthentication = false; + extraConfig = '' + AllowTcpForwarding yes + X11Forwarding no + AllowAgentForwarding no + AllowStreamLocalForwarding no + AuthenticationMethods publickey + ''; + }; + + # PCI compliance + environment.systemPackages = with pkgs; [ clamav ]; +} diff --git a/common/paranoid.nix b/common/paranoid.nix new file mode 100644 index 0000000..6e4a685 --- /dev/null +++ b/common/paranoid.nix @@ -0,0 +1,64 @@ +{ config, pkgs, lib, ... }: + +with lib; + +let + sources = import ../nix/sources.nix; + impermanence = sources.impermanence; + cfg = config.xeserv.paranoid; + + ifNoexec = if cfg.noexec then [ "noexec" ] else [ ]; +in { + imports = [ "${impermanence}/nixos.nix" ]; + + options.xeserv.paranoid = { + enable = mkEnableOption "enables ephemeral filesystems and limited persistence"; + noexec = mkEnableOption "enables every mount on the system save /nix being marked as noexec (potentially dangerous at a social level)"; + }; + + config = mkIf cfg.enable { + fileSystems."/" = mkForce { + device = "none"; + fsType = "tmpfs"; + options = [ "defaults" "size=2G" "mode=755" ] ++ ifNoexec; + }; + + fileSystems."/etc/nixos".options = ifNoexec; + fileSystems."/srv".options = ifNoexec; + fileSystems."/var/lib".options = ifNoexec; + fileSystems."/var/log".options = ifNoexec; + + fileSystems."/boot" = { + device = "/dev/disk/by-label/boot"; + fsType = "vfat"; + }; + + fileSystems."/nix" = { + device = "/dev/disk/by-label/nix"; + autoResize = true; + fsType = "ext4"; + }; + + boot.cleanTmpDir = true; + + environment.persistence."/nix/persist" = { + directories = [ + "/etc/nixos" # nixos system config files, can be considered optional + "/srv" # service data + "/var/lib" # system service persistent data + "/var/log" # the place that journald dumps it logs to + ]; + }; + + environment.etc."ssh/ssh_host_rsa_key".source = + "/nix/persist/etc/ssh/ssh_host_rsa_key"; + environment.etc."ssh/ssh_host_rsa_key.pub".source = + "/nix/persist/etc/ssh/ssh_host_rsa_key.pub"; + environment.etc."ssh/ssh_host_ed25519_key".source = + "/nix/persist/etc/ssh/ssh_host_ed25519_key"; + environment.etc."ssh/ssh_host_ed25519_key.pub".source = + "/nix/persist/etc/ssh/ssh_host_ed25519_key.pub"; + environment.etc."machine-id".source = "/nix/persist/etc/machine-id"; + }; +} + diff --git a/common/users.nix b/common/users.nix new file mode 100644 index 0000000..403daa0 --- /dev/null +++ b/common/users.nix @@ -0,0 +1,50 @@ +{ config, lib, pkgs, ... }: + +with lib; + +let + # Mara\ these options will be used for user account defaults in + # the `mkUser` function. + xeserv.users = { + groups = mkOption { + type = types.listOf types.str; + default = [ "wheel" ]; + example = ''[ "wheel" "libvirtd" "docker" ]''; + description = + "The Unix groups that Xeserv staff users should be assigned to"; + }; + + shell = mkOption { + type = types.package; + default = pkgs.bashInteractive; + example = "pkgs.powershell"; + description = + "The default shell that Xeserv staff users will be given by default."; + }; + }; + + cfg = config.xeserv.users; + + mkUser = { keys, shell ? cfg.shell, extraGroups ? cfg.groups, ... }: { + isNormalUser = true; + inherit extraGroups shell; + openssh.authorizedKeys = { + inherit keys; + }; + }; +in { + options.xeserv.users = xeserv.users; + + config.users.users = { + cadey = mkUser { + keys = [ + "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPg9gYKVglnO2HQodSJt4z4mNrUSUiyJQ7b+J798bwD9" + ]; + }; + twi = mkUser { + keys = [ + "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPYr9hiLtDHgd6lZDgQMkJzvYeAXmePOrgFaWHAjJvNU" + ]; + }; + }; +} diff --git a/images/build.nix b/images/build.nix new file mode 100644 index 0000000..1575429 --- /dev/null +++ b/images/build.nix @@ -0,0 +1,9 @@ +let + sources = import ../nix/sources.nix; + pkgs = import sources.nixpkgs { }; + sys = (import "${sources.nixpkgs}/nixos/lib/eval-config.nix" { + system = "x86_64-linux"; + modules = [ ./configuration.nix ]; + }); +in sys.config.system.build.toplevel + diff --git a/images/configuration.nix b/images/configuration.nix new file mode 100644 index 0000000..fa35568 --- /dev/null +++ b/images/configuration.nix @@ -0,0 +1,8 @@ +{ config, pkgs, lib, modulesPath, ... }: + +{ + imports = [ ../common (modulesPath + "/virtualisation/amazon-image.nix") ]; + + xeserv.paranoid.enable = true; +} + diff --git a/images/make-image.nix b/images/make-image.nix new file mode 100644 index 0000000..2dc0a78 --- /dev/null +++ b/images/make-image.nix @@ -0,0 +1,182 @@ +{ pkgs +, lib +, # The NixOS configuration to be installed onto the disk image. + config + +, # The size of the disk, in megabytes. + diskSize ? 8192 + +, # size of the boot partition, is only used if partitionTableType is + # either "efi" or "hybrid" + bootSize ? 512 + +, # The files and directories to be placed in the target file system. + # This is a list of attribute sets {source, target} where `source' + # is the file system object (regular file or directory) to be + # grafted in the file system at path `target'. + contents ? [] + +, # The initial NixOS configuration file to be copied to + # /etc/nixos/configuration.nix. This configuration will be embedded + # inside a configuration which includes the described ZFS fileSystems. + configFile ? null + +, # Shell code executed after the VM has finished. + postVM ? "" + +, name ? "nixos-disk-image" + +, # Disk image format, one of qcow2, qcow2-compressed, vdi, vpc, raw. + format ? "raw" + +, # Include a copy of Nixpkgs in the disk image + includeChannel ? false +, ... +}: +let + formatOpt = if format == "qcow2-compressed" then "qcow2" else format; + + compress = lib.optionalString (format == "qcow2-compressed") "-c"; + + filename = "nixos." + { + qcow2 = "qcow2"; + vdi = "vdi"; + vpc = "vhd"; + raw = "img"; + }.${formatOpt} or formatOpt; + + channelSources = + let + nixpkgs = lib.cleanSource pkgs.path; + in + pkgs.runCommand "nixos-${config.system.nixos.version}" {} '' + mkdir -p $out + cp -prd ${nixpkgs.outPath} $out/nixos + chmod -R u+w $out/nixos + if [ ! -e $out/nixos/nixpkgs ]; then + ln -s . $out/nixos/nixpkgs + fi + rm -rf $out/nixos/.git + echo -n ${config.system.nixos.versionSuffix} > $out/nixos/.version-suffix + ''; + + closureInfo = pkgs.closureInfo { + rootPaths = [ config.system.build.toplevel ] + ++ (lib.optional includeChannel channelSources); + }; + + modulesTree = pkgs.aggregateModules + (with config.boot.kernelPackages; [ kernel ]); + + tools = lib.makeBinPath ( + with pkgs; [ + config.system.build.nixos-enter + config.system.build.nixos-install + dosfstools + e2fsprogs + nix + parted + utillinux + ] + ); + + stringifyProperties = prefix: properties: lib.concatStringsSep " \\\n" ( + lib.mapAttrsToList + ( + property: value: "${prefix} ${lib.escapeShellArg property}=${lib.escapeShellArg value}" + ) + properties + ); + + image = ( + pkgs.vmTools.override { + rootModules = + [ "9p" "9pnet_virtio" "virtio_pci" "virtio_blk" "rtc_cmos" ]; + kernel = modulesTree; + } + ).runInLinuxVM ( + pkgs.runCommand name + { + preVM = '' + PATH=$PATH:${pkgs.qemu_kvm}/bin + mkdir $out + diskImage=nixos.raw + qemu-img create -f raw $diskImage ${toString diskSize}M + ''; + + postVM = '' + ${if formatOpt == "raw" then '' + mv $diskImage $out/${filename} + '' else '' + ${pkgs.qemu}/bin/qemu-img convert -f raw -O ${formatOpt} ${compress} $diskImage $out/${filename} + ''} + diskImage=$out/${filename} + ${postVM} + ''; + } '' + export PATH=${tools}:$PATH + set -x + + cp -sv /dev/vda /dev/sda + cp -sv /dev/vda /dev/xvda + + parted --script /dev/vda -- \ + mklabel gpt \ + mkpart no-fs 1MB 2MB \ + align-check optimal 1 \ + set 1 bios_grub on \ + mkpart ESP fat32 8MB ${toString bootSize}MB \ + align-check optimal 2 \ + set 2 boot on \ + mkpart primary ${toString bootSize}MB -1 \ + align-check optimal 3 \ + print + + mkdir /mnt + mount -t tmpfs none /mnt + + mkdir -p /mnt/{boot,nix,etc/{nixos,ssh},var/{lib,log},srv} + + mkdir -p /mnt/boot + mkfs.vfat /dev/vda2 -n boot + mount -t vfat /dev/vda2 /mnt/boot + + mkfs.ext4 -L nix /dev/vda3 + mount /dev/vda3 /mnt/nix + + mkdir -p /mnt/nix/persist/{etc/{nixos,ssh},var/{lib,log},srv} + + mount -o bind /mnt/nix/persist/etc/nixos /mnt/etc/nixos + mount -o bind /mnt/nix/persist/var/lib /mnt/var/lib + mount -o bind /mnt/nix/persist/var/log /mnt/var/log + + mount + + # Install a configuration.nix + mkdir -p /mnt/etc/nixos + # `cat` so it is mutable on the fs + cat ${./configuration.nix} > /mnt/etc/nixos/configuration.nix + cat ${./hardware-configuration.nix} > /mnt/etc/nixos/hardware-configuration.nix + + export NIX_STATE_DIR=$TMPDIR/state + nix-store --load-db < ${closureInfo}/registration + + echo copying toplevel + time nix copy --no-check-sigs --to 'local?root=/mnt/' ${config.system.build.toplevel} + + ${lib.optionalString includeChannel '' + echo copying channels + time nix copy --no-check-sigs --to 'local?root=/mnt/' ${channelSources} + ''} + + echo installing bootloader + time nixos-install --root /mnt --no-root-passwd \ + --system ${config.system.build.toplevel} \ + --substituters " " ${lib.optionalString includeChannel "--channel ${channelSources}"} + + df -h + umount /mnt/boot + '' + ); +in +image diff --git a/images/result b/images/result new file mode 120000 index 0000000..224e844 --- /dev/null +++ b/images/result @@ -0,0 +1 @@ +/nix/store/37g7indpvgc8pyismi03g1vcsyzvpdgc-nixos-system-unnamed-21.05pre-git \ No newline at end of file diff --git a/nix/sources.json b/nix/sources.json new file mode 100644 index 0000000..cc9f48b --- /dev/null +++ b/nix/sources.json @@ -0,0 +1,38 @@ +{ + "impermanence": { + "branch": "master", + "description": "Modules to help you handle persistent state on systems with ephemeral root storage [maintainer=@talyz]", + "homepage": "", + "owner": "nix-community", + "repo": "impermanence", + "rev": "58558845bc68dcf2bb32caa80564f7fe3f6cbc61", + "sha256": "10z3g4knkvq838zbfq71pkfyl8cffrpavna448wf5mjscycp0gnv", + "type": "tarball", + "url": "https://github.com/nix-community/impermanence/archive/58558845bc68dcf2bb32caa80564f7fe3f6cbc61.tar.gz", + "url_template": "https://github.com///archive/.tar.gz" + }, + "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///archive/.tar.gz" + }, + "nixpkgs": { + "branch": "nixos-21.05", + "description": "Nix Packages collection", + "homepage": "", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "2d6ab6c6b92f7aaf8bc53baba9754b9bfdce56f2", + "sha256": "1aafqly1mcqxh0r15mrlsrs4znldhm7cizsmfp3d25lqssay6gjd", + "type": "tarball", + "url": "https://github.com/NixOS/nixpkgs/archive/2d6ab6c6b92f7aaf8bc53baba9754b9bfdce56f2.tar.gz", + "url_template": "https://github.com///archive/.tar.gz" + } +} diff --git a/nix/sources.nix b/nix/sources.nix new file mode 100644 index 0000000..1938409 --- /dev/null +++ b/nix/sources.nix @@ -0,0 +1,174 @@ +# This file has been generated by Niv. + +let + + # + # The fetchers. fetch_ fetches specs of 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 = == ./.; + in + if builtins.hasAttr "nixpkgs" sources + then sourcesNixpkgs + else if hasNixpkgsPath && ! hasThisAsNixpkgsPath then + import {} + else + abort + '' + Please specify either (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); } diff --git a/shell.nix b/shell.nix new file mode 100644 index 0000000..20d11e7 --- /dev/null +++ b/shell.nix @@ -0,0 +1,10 @@ +{ pkgs ? import {} }: + +pkgs.mkShell { + buildInputs = [ + pkgs.hello + + # keep this line if you use bash + pkgs.bashInteractive + ]; +}