fix: writers & overlays

This commit is contained in:
2025-10-11 14:56:25 +00:00
parent 65fa944df8
commit 6dd7d112c5
10 changed files with 111 additions and 236 deletions

View File

@@ -42,15 +42,31 @@
}@inputs: let
flake = ./.;
nixpkgs = nixpkgs-25-05;
overlays = [ self.overlays.default ];
self-lib = import ./lib { inherit flake self inputs; };
# Create overlay that includes legacy packages
overlayWithLegacy = system: final: prev:
let
baseOverlay = (import ./overlay { inherit flake self inputs nixpkgs; }) final prev;
legacyPackages = import ./legacy { inherit system pkgs self; };
pkgs = import nixpkgs {
inherit system;
overlays = [ (import rust-overlay) ];
};
in
baseOverlay // legacyPackages;
overlays = [ self.overlays.default ];
in self-lib.forAllSystemsWithPkgs ([(import rust-overlay)] ++ overlays) ({
system,
pkgs,
}: {
packages.${system} = import ./package { inherit system pkgs self; };
legacyPackages.${system} = import ./legacy { inherit system pkgs self; };
devShells.${system} = import ./devshell { inherit system pkgs self; };
packages.${system} = import ./package { inherit system self pkgs; };
devShells.${system} = import ./devshell { inherit system self pkgs; };
legacyPackages.${system} = import ./legacy {
inherit system self;
pkgs = import nixpkgs { inherit system; };
};
nixosConfigurations = {
"devvm-manual|${system}" = import ./nixos/system/devvm-manual/default.nix
{ inherit flake self inputs system; };

View File

@@ -1,4 +1,7 @@
{ self, system, pkgs }:
{
let
writers = pkgs.callPackage ./writer { };
}
in {
# NOTE(yukkop): duplicate writers in root of legacyPackages and writers due nixpkgs legacyPackages consistency
writers = writers;
} // writers

View File

@@ -1,41 +1,11 @@
{
lib,
writers,
gcc,
}: let
writeC = name: argsOrScript:
if lib.isAttrs argsOrScript && !lib.isDerivation argsOrScript
then
writers.makeBinWriter (
argsOrScript
// {
compileScript = ''
# Force gcc to treat the input file as C code
${gcc}/bin/gcc -fsyntax-only -xc $contentPath
if [ $? -ne 0 ]; then
echo "Syntax check failed"
exit 1
fi
${gcc}/bin/gcc -xc -o $out $contentPath
'';
}
)
name
else
writers.makeBinWriter {
compileScript = ''
# Force gcc to treat the input file as C code
${gcc}/bin/gcc -fsyntax-only -xc $contentPath
if [ $? -ne 0 ]; then
echo "Syntax check failed"
exit 1
fi
${gcc}/bin/gcc -xc -o $out $contentPath
'';
}
name
argsOrScript;
writeMinC = name: includes: body:
callPackage
}: rec {
writeShellApplication = callPackage ./writeShellApplication.nix {};
writeC = callPackage ./writeC.nix {};
writeCBin = name: writeC "/bin/${name}";
writeMinCBin = name: includes: body: writeMinC "/bin/${name}" includes body;
writeMinC = name: includes: body:
writeC name ''
${builtins.concatStringsSep "\n" (map (h: "#include " + h) includes)}
@@ -43,9 +13,4 @@
${body}
}
'';
in {
writeCBin = name: writeC "/bin/${name}";
writeC = writeC;
writeMinCBin = name: includes: body: writeMinC "/bin/${name}" includes body;
writeMinC = writeMinC;
}

35
legacy/writer/writeC.nix Normal file
View File

@@ -0,0 +1,35 @@
{ lib, writers, gcc }:
name: argsOrScript:
if
lib.isAttrs argsOrScript
&& !lib.isDerivation argsOrScript
then
writers.makeBinWriter (
argsOrScript
// {
compileScript = ''
# Force gcc to treat the input file as C code
${gcc}/bin/gcc -fsyntax-only -xc $contentPath
if [ $? -ne 0 ]; then
echo "Syntax check failed"
exit 1
fi
${gcc}/bin/gcc -xc -o $out $contentPath
'';
}
)
name
else
writers.makeBinWriter {
compileScript = ''
# Force gcc to treat the input file as C code
${gcc}/bin/gcc -fsyntax-only -xc $contentPath
if [ $? -ne 0 ]; then
echo "Syntax check failed"
exit 1
fi
${gcc}/bin/gcc -xc -o $out $contentPath
'';
}
name
argsOrScript

View File

@@ -1,3 +1,10 @@
{
writeTextFile,
lib,
shellcheck-minimal,
stdenv,
runtimeShell,
}:
{
/*
The name of the script to write.
@@ -87,6 +94,8 @@
Type: Bool
*/
inheritPath ? true,
shell ? runtimeShell,
}:
writeTextFile {
inherit
@@ -99,27 +108,26 @@ writeTextFile {
destination = "/bin/${name}";
allowSubstitutes = true;
preferLocalBuild = false;
text =
''
#!${dash}/bin/dash
${lib.concatMapStringsSep "\n" (option: "set -o ${option}") bashOptions}
''
+ lib.optionalString (runtimeEnv != null) (
lib.concatStrings (
lib.mapAttrsToList (name: value: ''
${lib.toShellVar name value}
export ${name}
'') runtimeEnv
)
text = ''
#!${shell}
${lib.concatMapStringsSep "\n" (option: "set -o ${option}") bashOptions}
''
+ lib.optionalString (runtimeEnv != null) (
lib.concatStrings (
lib.mapAttrsToList (name: value: ''
${lib.toShellVar name value}
export ${name}
'') runtimeEnv
)
+ lib.optionalString (runtimeInputs != [ ]) ''
)
+ lib.optionalString (runtimeInputs != [ ]) ''
export PATH="${lib.makeBinPath runtimeInputs}${lib.optionalString inheritPath ":$PATH"}"
''
+ ''
export PATH="${lib.makeBinPath runtimeInputs}${lib.optionalString inheritPath ":$PATH"}"
''
+ ''
${text}
'';
${text}
'';
checkPhase =
let
@@ -140,11 +148,10 @@ writeTextFile {
if checkPhase == null then
''
runHook preCheck
#dryrun
${dash}/bin/dash -n -O extglob "$target"
${stdenv.shellDryRun} "$target"
${shellcheckCommand}
runHook postCheck
''
else
checkPhase;
};
}

View File

@@ -49,7 +49,7 @@ in {
extensions = let
packages = {
inherit (cfg.package.pkgs) pg_net pgjwt pg_cron http pg_smtp_client plsh;
hemar = inputs.hutil.packages.${system}.pg-15-hemar;
hemar = self.packages.${system}.pg-15-hemar;
};
in
lib.attrValues (

View File

@@ -5,7 +5,7 @@ in final: prev: (
packages = self.packages.${prev.system};
legacyPackages = self.legacyPackages.${prev.system};
in {
hectic = packages;
hectic = packages // legacyPackages;
postgresql_17 = prev.postgresql_17 // {pkgs = prev.postgresql_17.pkgs // {
http = packages.pg-17-ext-http;
pg_smtp_client = packages.pg-17-ext-smtp-client;
@@ -27,6 +27,5 @@ in final: prev: (
plsh = packages.pg-15-ext-plsh;
hemar = packages.pg-15-ext-hemar;
};};
writers = prev.writers // legacyPackages.writers;
}
)

View File

@@ -1,150 +0,0 @@
{
/*
The name of the script to write.
Type: String
*/
name,
/*
The shell script's text, not including a shebang.
Type: String
*/
text,
/*
Inputs to add to the shell script's `$PATH` at runtime.
Type: [String|Derivation]
*/
runtimeInputs ? [ ],
/*
Extra environment variables to set at runtime.
Type: AttrSet
*/
runtimeEnv ? null,
/*
`stdenv.mkDerivation`'s `meta` argument.
Type: AttrSet
*/
meta ? { },
/*
`stdenv.mkDerivation`'s `passthru` argument.
Type: AttrSet
*/
passthru ? { },
/*
The `checkPhase` to run. Defaults to `shellcheck` on supported
platforms and `bash -n`.
The script path will be given as `$target` in the `checkPhase`.
Type: String
*/
checkPhase ? null,
/*
Checks to exclude when running `shellcheck`, e.g. `[ "SC2016" ]`.
See <https://www.shellcheck.net/wiki/> for a list of checks.
Type: [String]
*/
excludeShellChecks ? [ ],
/*
Extra command-line flags to pass to ShellCheck.
Type: [String]
*/
extraShellCheckFlags ? [ ],
/*
Bash options to activate with `set -o` at the start of the script.
Defaults to `[ "errexit" "nounset" "pipefail" ]`.
Type: [String]
*/
bashOptions ? [
"errexit"
"nounset"
"pipefail"
],
/*
Extra arguments to pass to `stdenv.mkDerivation`.
:::{.caution}
Certain derivation attributes are used internally,
overriding those could cause problems.
:::
Type: AttrSet
*/
derivationArgs ? { },
/*
Whether to inherit the current `$PATH` in the script.
Type: Bool
*/
inheritPath ? true,
}:
writeTextFile {
inherit
name
meta
passthru
derivationArgs
;
executable = true;
destination = "/bin/${name}";
allowSubstitutes = true;
preferLocalBuild = false;
text =
''
#!${dash}/bin/dash
${lib.concatMapStringsSep "\n" (option: "set -o ${option}") bashOptions}
''
+ lib.optionalString (runtimeEnv != null) (
lib.concatStrings (
lib.mapAttrsToList (name: value: ''
${lib.toShellVar name value}
export ${name}
'') runtimeEnv
)
)
+ lib.optionalString (runtimeInputs != [ ]) ''
export PATH="${lib.makeBinPath runtimeInputs}${lib.optionalString inheritPath ":$PATH"}"
''
+ ''
${text}
'';
checkPhase =
let
excludeFlags = lib.optionals (excludeShellChecks != [ ]) [
"--exclude"
(lib.concatStringsSep "," excludeShellChecks)
];
# GHC (=> shellcheck) isn't supported on some platforms (such as risc-v)
# but we still want to use writeShellApplication on those platforms
shellcheckCommand = lib.optionalString shellcheck-minimal.compiler.bootstrapAvailable ''
# use shellcheck which does not include docs
# pandoc takes long to build and documentation isn't needed for just running the cli
${lib.getExe shellcheck-minimal} ${
lib.escapeShellArgs (excludeFlags ++ extraShellCheckFlags)
} "$target"
'';
in
if checkPhase == null then
''
runHook preCheck
#dryrun
${dash}/bin/dash -n -O extglob "$target"
${shellcheckCommand}
runHook postCheck
''
else
checkPhase;
};

View File

@@ -1,25 +1,35 @@
{ symlinkJoin, writeShellApplication, socat, dash, hectic, curl }:
let
base64 = writeShellApplication {
shell = "${dash}/bin/dash";
bashOptions = [
"errexit"
"nounset"
];
base64 = hectic.writeShellApplication {
inherit shell bashOptions;
name = "base64";
runtimeInputs = [ ];
text = builtins.readFile ./base64.sh;
};
# TODO: writeDashApplication
probe = writeShellApplication {
probe = hectic.writeShellApplication {
inherit shell bashOptions;
name = "probe";
runtimeInputs = [ socat dash probe-loop ];
text = builtins.readFile ./probe.sh;
};
probe-loop = writeShellApplication {
probe-loop = hectic.writeShellApplication {
inherit shell bashOptions;
name = "probe-loop";
runtimeInputs = [ base64 ];
text = builtins.readFile ./probe-loop.sh;
};
sentinel = writeShellApplication {
sentinel = hectic.writeShellApplication {
inherit shell bashOptions;
name = "sentinel";
runtimeInputs = [ hectic.shellplot curl ];
text = builtins.readFile ./sentinel.sh;

View File

@@ -1,15 +1,5 @@
#!/usr/bin/env dash
#
#
#
#
#
#
#
#
set -euo pipefail
socat -V >/dev/null
dash -c 'echo ok' >/dev/null