diff --git a/.envrc b/.envrc index 3550a30..1d953f4 100644 --- a/.envrc +++ b/.envrc @@ -1 +1 @@ -use flake +use nix diff --git a/Dockerfile b/Dockerfile index 5d4462d..18554fc 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ # Compile application with the official Rust image -FROM --platform=$BUILDPLATFORM rust:1.93.0-alpine3.23 AS builder +FROM --platform=$BUILDPLATFORM rust:1.93.1-alpine3.23 AS builder ENV PKGCONFIG_SYSROOTDIR=/ # Add build dependencies and targets RUN apk add --no-cache musl-dev libressl-dev perl build-base zig diff --git a/default.nix b/default.nix new file mode 100644 index 0000000..5ffb054 --- /dev/null +++ b/default.nix @@ -0,0 +1,4 @@ +{ + system ? builtins.currentSystem, +}: +(import ./lib.nix { inherit system; }).sandhole diff --git a/flake.lock b/flake.lock index 9b5dd31..5999137 100644 --- a/flake.lock +++ b/flake.lock @@ -1,61 +1,6 @@ { "nodes": { - "crane": { - "locked": { - "lastModified": 1767744144, - "narHash": "sha256-9/9ntI0D+HbN4G0TrK3KmHbTvwgswz7p8IEJsWyef8Q=", - "owner": "ipetkov", - "repo": "crane", - "rev": "2fb033290bf6b23f226d4c8b32f7f7a16b043d7e", - "type": "github" - }, - "original": { - "owner": "ipetkov", - "repo": "crane", - "type": "github" - } - }, - "nixpkgs": { - "locked": { - "lastModified": 1769740369, - "narHash": "sha256-xKPyJoMoXfXpDM5DFDZDsi9PHArf2k5BJjvReYXoFpM=", - "owner": "NixOS", - "repo": "nixpkgs", - "rev": "6308c3b21396534d8aaeac46179c14c439a89b8a", - "type": "github" - }, - "original": { - "owner": "NixOS", - "ref": "nixpkgs-unstable", - "repo": "nixpkgs", - "type": "github" - } - }, - "root": { - "inputs": { - "crane": "crane", - "nixpkgs": "nixpkgs", - "rust-overlay": "rust-overlay" - } - }, - "rust-overlay": { - "inputs": { - "nixpkgs": ["nixpkgs"] - }, - "locked": { - "lastModified": 1769828398, - "narHash": "sha256-zmnvRUm15QrlKH0V1BZoiT3U+Q+tr+P5Osi8qgtL9fY=", - "owner": "oxalica", - "repo": "rust-overlay", - "rev": "a1d32c90c8a4ea43e9586b7e5894c179d5747425", - "type": "github" - }, - "original": { - "owner": "oxalica", - "repo": "rust-overlay", - "type": "github" - } - } + "root": {} }, "root": "root", "version": 7 diff --git a/flake.nix b/flake.nix index 1b36c59..25191cd 100644 --- a/flake.nix +++ b/flake.nix @@ -1,23 +1,10 @@ { description = "Expose HTTP/SSH/TCP services through SSH port forwarding"; - inputs = { - nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; - crane.url = "github:ipetkov/crane"; - rust-overlay = { - url = "github:oxalica/rust-overlay"; - inputs.nixpkgs.follows = "nixpkgs"; - }; - }; + inputs = { }; outputs = - { - self, - nixpkgs, - crane, - rust-overlay, - ... - }: + { self, ... }: let systems = [ "x86_64-linux" @@ -48,13 +35,13 @@ sandhole = { lib, pkgs, ... }: { - imports = [ ./nixos/module.nix ]; + imports = [ ./nix/modules/sandhole.nix ]; services.sandhole.package = lib.mkDefault self.packages.${pkgs.stdenv.hostPlatform.system}.default; }; sandhole-websites = { ... }: { - imports = [ ./nixos/sandhole-websites.nix ]; + imports = [ ./nix/modules/sandhole-websites.nix ]; }; }; @@ -68,97 +55,14 @@ // eachSystem ( system: let - rustChannel = "stable"; - rustVersion = "latest"; - - pkgs = import nixpkgs { - inherit system; - overlays = [ (import rust-overlay) ]; - }; - - inherit (pkgs) lib; - - craneLib = (crane.mkLib pkgs).overrideToolchain ( - pkgs: pkgs.rust-bin.${rustChannel}.${rustVersion}.default - ); - - src = lib.fileset.toSource { - root = ./.; - fileset = lib.fileset.unions [ - (craneLib.fileset.commonCargoSources ./.) - ./README.md - ./.config/nextest.toml - ./tests/data - ]; - }; - - commonArgs = { - inherit src; - strictDeps = true; - - nativeBuildInputs = [ - pkgs.cmake - pkgs.perl - ]; - }; - - cargoArtifacts = craneLib.buildDepsOnly commonArgs; - - sandhole = - (craneLib.buildPackage ( - commonArgs - // { - inherit cargoArtifacts; - doCheck = false; - } - )) - // { - meta.mainProgram = "sandhole"; - }; - - evalOptions = lib.evalModules { - modules = [ - ( - { config, ... }: - { - options = - (import ./nixos/module.nix { - inherit pkgs lib; - config = config; - }).options; - } - ) - ]; - }; - - optionsDoc = pkgs.nixosOptionsDoc { - options = removeAttrs evalOptions.options [ "_module" ]; - }; - - sandhole-cli = pkgs.stdenv.mkDerivation { - name = "sandhole-cli"; - nativeBuildInputs = [ pkgs.to-html ]; - buildCommand = '' - mkdir $out - to-html --no-prompt "${lib.getExe sandhole} --help" > $out/cli.html - ''; - }; - - sandhole-book = pkgs.stdenv.mkDerivation { - name = "sandhole-book"; - src = lib.fileset.toSource { - root = ./.; - fileset = lib.fileset.unions [ - ./book/book.toml - ./book/src - ./book/theme - ]; - }; - nativeBuildInputs = [ pkgs.mdbook ]; - buildPhase = '' - mdbook build book --dest-dir $out - ''; - }; + inherit (import ./lib.nix { inherit system; }) + sandhole + sandhole-book + sandhole-cli + optionsDoc + lib + checks + ; in { packages.${system} = { @@ -182,100 +86,9 @@ }; }; - checks.${system} = { - inherit sandhole; + checks.${system} = checks; - sandhole-clippy = craneLib.cargoClippy ( - commonArgs - // { - inherit cargoArtifacts; - } - ); - - sandhole-doc = craneLib.cargoDoc ( - commonArgs - // { - inherit cargoArtifacts; - } - ); - - sandhole-fmt = craneLib.cargoFmt { - inherit src; - }; - - sandhole-test = - let - sandhole-nextest-archive = craneLib.mkCargoDerivation ( - commonArgs - // { - inherit cargoArtifacts; - pname = "sandhole-nextest-archive"; - doCheck = false; - nativeBuildInputs = commonArgs.nativeBuildInputs ++ [ pkgs.cargo-nextest ]; - buildPhaseCargoCommand = '' - cargo nextest archive --archive-format tar-zst --archive-file archive.tar.zst - ''; - installPhaseCommand = '' - mkdir -p $out - cp archive.tar.zst $out - ''; - } - ); - in - pkgs.testers.runNixOSTest { - name = "sandhole-nextest"; - nodes = { - machine = - { ... }: - { - virtualisation.diskSize = 4096; - environment.defaultPackages = [ - pkgs.cargo - pkgs.rustc - ]; - systemd.services.sandhole-nextest = { - description = "Sandhole tests"; - wantedBy = [ "multi-user.target" ]; - after = [ "network-online.target" ]; - wants = [ "network-online.target" ]; - script = '' - cp -r ${src}/* . - ${pkgs.cargo-nextest}/bin/cargo-nextest nextest run \ - --archive-file ${sandhole-nextest-archive}/archive.tar.zst \ - --workspace-remap . - ''; - serviceConfig = { - StateDirectory = "sandhole-nextest"; - StateDirectoryMode = "0750"; - WorkingDirectory = "/var/lib/sandhole-nextest"; - Type = "oneshot"; - RemainAfterExit = "yes"; - Restart = "no"; - }; - }; - }; - }; - testScript = '' - machine.start() - machine.wait_for_unit("sandhole-nextest.service") - ''; - }; - }; - - devShells.${system}.default = craneLib.devShell { - checks = self.checks.${system}; - - inputsFrom = [ sandhole ]; - - packages = [ - pkgs.cargo-flamegraph - pkgs.cargo-nextest - pkgs.just - pkgs.mdbook - pkgs.minica - pkgs.to-html - ]; - }; + devShells.${system}.default = import ./shell.nix { inherit system; }; } ); } diff --git a/lib.nix b/lib.nix new file mode 100644 index 0000000..b3f0b04 --- /dev/null +++ b/lib.nix @@ -0,0 +1,188 @@ +{ + system ? builtins.currentSystem, + rustChannel ? "stable", + rustVersion ? "latest", +}: +let + sources = import ./npins; + + pkgs = import sources.nixpkgs { + inherit system; + overlays = [ (import sources.rust-overlay) ]; + }; + + inherit (pkgs) lib; + + craneLib = (import sources.crane { inherit pkgs; }).overrideToolchain ( + p: p.rust-bin.${rustChannel}.${rustVersion}.default + ); + + src = lib.fileset.toSource { + root = ./.; + fileset = lib.fileset.unions [ + (craneLib.fileset.commonCargoSources ./.) + ./README.md + ./.config/nextest.toml + ./tests/data + ]; + }; + + commonArgs = { + inherit src; + strictDeps = true; + + nativeBuildInputs = [ + pkgs.cmake + pkgs.perl + ]; + }; + + cargoArtifacts = craneLib.buildDepsOnly commonArgs; + + sandhole = craneLib.buildPackage ( + commonArgs + // { + inherit cargoArtifacts; + doCheck = false; + meta.mainProgram = "sandhole"; + } + ); + + evalOptions = lib.evalModules { + modules = [ + ( + { config, ... }: + { + options = + (import ./nix/modules/sandhole.nix { + inherit pkgs lib; + config = config; + }).options; + } + ) + ]; + }; +in +{ + inherit + pkgs + lib + craneLib + commonArgs + cargoArtifacts + sandhole + ; + + optionsDoc = pkgs.nixosOptionsDoc { + options = removeAttrs evalOptions.options [ "_module" ]; + }; + + sandhole-cli = pkgs.stdenv.mkDerivation { + name = "sandhole-cli"; + nativeBuildInputs = [ pkgs.to-html ]; + buildCommand = '' + mkdir $out + to-html --no-prompt "${lib.getExe sandhole} --help" > $out/cli.html + ''; + }; + + sandhole-book = pkgs.stdenv.mkDerivation { + name = "sandhole-book"; + src = lib.fileset.toSource { + root = ./.; + fileset = lib.fileset.unions [ + ./book/book.toml + ./book/src + ./book/theme + ]; + }; + nativeBuildInputs = [ pkgs.mdbook ]; + buildPhase = '' + mdbook build book --dest-dir $out + ''; + }; + + checks = { + inherit sandhole; + + sandhole-clippy = craneLib.cargoClippy ( + commonArgs + // { + inherit cargoArtifacts; + } + ); + + sandhole-doc = craneLib.cargoDoc ( + commonArgs + // { + inherit cargoArtifacts; + } + ); + + sandhole-fmt = craneLib.cargoFmt { + inherit src; + }; + + sandhole-test = + let + sandhole-nextest-archive = craneLib.mkCargoDerivation ( + commonArgs + // { + inherit cargoArtifacts; + pname = "sandhole-nextest-archive"; + doCheck = false; + nativeBuildInputs = (commonArgs.nativeBuildInputs or [ ]) ++ [ pkgs.cargo-nextest ]; + buildPhaseCargoCommand = '' + cargo nextest archive --archive-format tar-zst --archive-file archive.tar.zst + ''; + installPhaseCommand = '' + mkdir -p $out + cp archive.tar.zst $out + ''; + } + ); + in + pkgs.testers.runNixOSTest { + name = "sandhole-nextest"; + nodes = { + machine = + { pkgs, ... }: + { + virtualisation.diskSize = 4096; + environment.defaultPackages = [ + pkgs.cargo + pkgs.rustc + ]; + systemd.services.sandhole-nextest = { + description = "Sandhole tests"; + wantedBy = [ "multi-user.target" ]; + after = [ "network-online.target" ]; + wants = [ "network-online.target" ]; + path = [ + pkgs.cargo + pkgs.cargo-nextest + ]; + script = '' + cp -r ${src}/* . + cargo nextest run \ + --archive-file ${sandhole-nextest-archive}/archive.tar.zst \ + --workspace-remap . + ''; + serviceConfig = { + StateDirectory = "sandhole-nextest"; + StateDirectoryMode = "0750"; + WorkingDirectory = "/var/lib/sandhole-nextest"; + Type = "oneshot"; + RemainAfterExit = "yes"; + Restart = "no"; + }; + }; + }; + }; + testScript = '' + machine.start() + machine.wait_for_unit("sandhole-nextest.service") + ''; + }; + }; +} diff --git a/nixos/sandhole-websites.nix b/nix/modules/sandhole-websites.nix similarity index 100% rename from nixos/sandhole-websites.nix rename to nix/modules/sandhole-websites.nix diff --git a/nixos/module.nix b/nix/modules/sandhole.nix similarity index 100% rename from nixos/module.nix rename to nix/modules/sandhole.nix diff --git a/nixos/docs.nix b/nixos/docs.nix deleted file mode 100644 index 19b55b5..0000000 --- a/nixos/docs.nix +++ /dev/null @@ -1,20 +0,0 @@ -flake: -let - lib = flake.lib; - pkgs = flake.self.nixosConfigurations.basic.pkgs; - - evalOptions = ( - lib.evalModules { - modules = [ - (import ./module.nix flake) - ]; - } - ); - - optionsDoc = ( - pkgs.nixosOptionsDoc { - options = removeAttrs evalOptions.options [ "_module" ]; - } - ); -in -optionsDoc.optionsCommonMark diff --git a/npins/default.nix b/npins/default.nix new file mode 100644 index 0000000..884fc8c --- /dev/null +++ b/npins/default.nix @@ -0,0 +1,249 @@ +/* + This file is provided under the MIT licence: + + Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ +# Generated by npins. Do not modify; will be overwritten regularly +let + # Backwards-compatibly make something that previously didn't take any arguments take some + # The function must return an attrset, and will unfortunately be eagerly evaluated + # Same thing, but it catches eval errors on the default argument so that one may still call it with other arguments + mkFunctor = + fn: + let + e = builtins.tryEval (fn { }); + in + (if e.success then e.value else { error = fn { }; }) // { __functor = _self: fn; }; + + # 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 ""; + + # 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 = + name: spec: + { + pkgs ? null, + }: + assert spec ? type; + let + # Unify across builtin and pkgs fetchers. + # `fetchGit` requires a wrapper because of slight API differences. + fetchers = + if pkgs == null then + { + inherit (builtins) fetchTarball fetchurl; + # For some fucking reason, fetchGit has a different signature than the other builtin fetchers … + fetchGit = args: (builtins.fetchGit args).outPath; + } + else + { + fetchTarball = + { + url, + sha256, + }: + pkgs.fetchzip { + inherit url sha256; + extension = "tar"; + }; + inherit (pkgs) fetchurl; + fetchGit = + { + url, + submodules, + rev, + name, + narHash, + }: + pkgs.fetchgit { + inherit url rev name; + fetchSubmodules = submodules; + hash = narHash; + }; + }; + + # Dispatch to the correct code path based on the type + path = + if spec.type == "Git" then + mkGitSource fetchers spec + else if spec.type == "GitRelease" then + mkGitSource fetchers spec + else if spec.type == "PyPi" then + mkPyPiSource fetchers spec + else if spec.type == "Channel" then + mkChannelSource fetchers spec + else if spec.type == "Tarball" then + mkTarballSource fetchers spec + else if spec.type == "Container" then + mkContainerSource pkgs spec + else + builtins.throw "Unknown source type ${spec.type}"; + in + spec // { outPath = mayOverride name path; }; + + mkGitSource = + { + fetchTarball, + fetchGit, + ... + }: + { + repository, + revision, + url ? null, + submodules, + hash, + ... + }: + assert repository ? type; + # At the moment, either it is a plain git repository (which has an url), or it is a GitHub/GitLab repository + # In the latter case, there we will always be an url to the tarball + if url != null && !submodules then + fetchTarball { + inherit url; + sha256 = hash; + } + else + let + url = + if repository.type == "Git" then + repository.url + else if repository.type == "GitHub" then + "https://github.com/${repository.owner}/${repository.repo}.git" + else if repository.type == "GitLab" then + "${repository.server}/${repository.repo_path}.git" + else if repository.type == "Forgejo" then + "${repository.server}/${repository.owner}/${repository.repo}.git" + else + throw "Unrecognized repository type ${repository.type}"; + urlToName = + url: rev: + let + matched = builtins.match "^.*/([^/]*)(\\.git)?$" url; + + short = builtins.substring 0 7 rev; + + appendShort = if (builtins.match "[a-f0-9]*" rev) != null then "-${short}" else ""; + in + "${if matched == null then "source" else builtins.head matched}${appendShort}"; + name = urlToName url revision; + in + fetchGit { + rev = revision; + narHash = hash; + + inherit name submodules url; + }; + + mkPyPiSource = + { fetchurl, ... }: + { + url, + hash, + ... + }: + fetchurl { + inherit url; + sha256 = hash; + }; + + mkChannelSource = + { fetchTarball, ... }: + { + url, + hash, + ... + }: + fetchTarball { + inherit url; + sha256 = hash; + }; + + mkTarballSource = + { fetchTarball, ... }: + { + url, + locked_url ? url, + hash, + ... + }: + fetchTarball { + url = locked_url; + sha256 = hash; + }; + + mkContainerSource = + pkgs: + { + image_name, + image_tag, + image_digest, + ... + }: + if pkgs == null then + builtins.throw "container sources require passing in a Nixpkgs value: https://github.com/andir/npins/blob/master/README.md#using-the-nixpkgs-fetchers" + else + pkgs.dockerTools.pullImage { + imageName = image_name; + imageDigest = image_digest; + finalImageTag = image_tag; + }; +in +mkFunctor ( + { + input ? ./sources.json, + }: + let + data = + if builtins.isPath input then + # while `readFile` will throw an error anyways if the path doesn't exist, + # we still need to check beforehand because *our* error can be caught but not the one from the builtin + # *piegames sighs* + if builtins.pathExists input then + builtins.fromJSON (builtins.readFile input) + else + throw "Input path ${toString input} does not exist" + else if builtins.isAttrs input then + input + else + throw "Unsupported input type ${builtins.typeOf input}, must be a path or an attrset"; + version = data.version; + in + if version == 7 then + builtins.mapAttrs (name: spec: mkFunctor (mkSource name spec)) data.pins + else + throw "Unsupported format version ${toString version} in sources.json. Try running `npins upgrade`" +) diff --git a/npins/sources.json b/npins/sources.json new file mode 100644 index 0000000..6fdc27c --- /dev/null +++ b/npins/sources.json @@ -0,0 +1,40 @@ +{ + "pins": { + "crane": { + "type": "GitRelease", + "repository": { + "type": "GitHub", + "owner": "ipetkov", + "repo": "crane" + }, + "pre_releases": false, + "version_upper_bound": null, + "release_prefix": null, + "submodules": false, + "version": "v0.23.0", + "revision": "61594d90dab41c2f3cd336baf0a8fcd6c37e0408", + "url": "https://api.github.com/repos/ipetkov/crane/tarball/refs/tags/v0.23.0", + "hash": "sha256-VFkNyxHxkqGp8gf8kfFMW1j6XeBy609kv6TE9uF/0Js=" + }, + "nixpkgs": { + "type": "Channel", + "name": "nixpkgs-unstable", + "url": "https://releases.nixos.org/nixpkgs/nixpkgs-26.05pre944764.2343bbb58f99/nixexprs.tar.xz", + "hash": "sha256-9SFCZkVcpDOV6unH5hVEy4+dB0rxMuUoBnDAO6vshac=" + }, + "rust-overlay": { + "type": "Git", + "repository": { + "type": "GitHub", + "owner": "oxalica", + "repo": "rust-overlay" + }, + "branch": "master", + "submodules": false, + "revision": "d7a86c8a4df49002446737603a3e0d7ef91a9637", + "url": "https://github.com/oxalica/rust-overlay/archive/d7a86c8a4df49002446737603a3e0d7ef91a9637.tar.gz", + "hash": "sha256-TygYZ7JhnJbRoWOk7d5HaA/GhEVCvtRruN7TqaN9s/c=" + } + }, + "version": 7 +} diff --git a/shell.nix b/shell.nix new file mode 100644 index 0000000..2b388f4 --- /dev/null +++ b/shell.nix @@ -0,0 +1,16 @@ +{ + system ? builtins.currentSystem, +}: +let + inherit (import ./lib.nix { inherit system; }) pkgs craneLib; +in +craneLib.devShell { + packages = [ + pkgs.cargo-flamegraph + pkgs.cargo-nextest + pkgs.just + pkgs.mdbook + pkgs.minica + pkgs.to-html + ]; +}