Skip to content

Commit

Permalink
feat(pops/scripts): add writeShellApplication
Browse files Browse the repository at this point in the history
  • Loading branch information
GTrunSec committed Nov 26, 2023
1 parent 5380ed3 commit 0b8d60a
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 29 deletions.
32 changes: 16 additions & 16 deletions examples/packages/_sources/generated.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
{
"flake8-black": {
"cargoLocks": null,
"date": null,
"extract": null,
"name": "flake8-black",
"passthru": null,
"pinned": false,
"src": {
"name": null,
"sha256": "sha256-DfvKMnR3d5KlvLKviHpMrXLHLQ6GyU4I46PeFRu0HDQ=",
"type": "url",
"url": "https://pypi.org/packages/source/f/flake8-black/flake8-black-0.3.6.tar.gz"
},
"version": "0.3.6"
}
}
"flake8-black": {
"cargoLocks": null,
"date": null,
"extract": null,
"name": "flake8-black",
"passthru": null,
"pinned": false,
"src": {
"name": null,
"sha256": "sha256-DfvKMnR3d5KlvLKviHpMrXLHLQ6GyU4I46PeFRu0HDQ=",
"type": "url",
"url": "https://pypi.org/packages/source/f/flake8-black/flake8-black-0.3.6.tar.gz"
},
"version": "0.3.6"
}
}
7 changes: 1 addition & 6 deletions examples/packages/_sources/generated.nix
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
# This file was generated by nvfetcher, please do not modify it manually.
{
fetchgit,
fetchurl,
fetchFromGitHub,
dockerTools,
}:
{ fetchgit, fetchurl, fetchFromGitHub, dockerTools }:
{
flake8-black = {
pname = "flake8-black";
Expand Down
61 changes: 61 additions & 0 deletions src/pops/_misc/writeShellApplication.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
_:
{ nixpkgs }:
let
l = nixpkgs.lib // builtins;
in
{
name,
text,
runtimeInputs ? [ ],
runtimeEnv ? { },
runtimeShell ? nixpkgs.runtimeShell,
checkPhase ? null,
}:
let
runtimeShell' =
if runtimeShell != nixpkgs.runtimeShell then
(l.getExe runtimeShell)
else
runtimeShell;
text' = if l.isPath text then nixpkgs.lib.fileContents text else text;
in
nixpkgs.writeTextFile {
inherit name;
executable = true;
destination = "/bin/${name}";
text =
''
#!${runtimeShell'}
# shellcheck shell=bash
set -o errexit
set -o nounset
set -o pipefail
''
+ l.optionalString (runtimeInputs != [ ]) ''
export PATH="${l.makeBinPath runtimeInputs}:$PATH"
''
+ l.optionalString (runtimeEnv != { }) ''
${l.concatStringsSep "\n" (
l.mapAttrsToList (n: v: "export ${n}=${''"$''}{${n}:-${toString v}}${''"''}")
runtimeEnv
)}
''
+ ''
${text'}
'';

checkPhase =
if checkPhase == null then
''
runHook preCheck
${nixpkgs.stdenv.shellDryRun} "$target"
${nixpkgs.shellcheck}/bin/shellcheck "$target"
runHook postCheck
''
else
checkPhase;

meta.mainProgram = name;
}
1 change: 1 addition & 0 deletions src/pops/scripts.nix
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
x
// {
inherit nixpkgs;
writeShellApplication = super.misc.writeShellApplication { inherit nixpkgs; };
}
// lib.optionalAttrs (x.inputs ? climodSrc) {
climod = nixpkgs.callPackage inputs.climodSrc { pkgs = nixpkgs; };
Expand Down
7 changes: 3 additions & 4 deletions units/scripts/nvfetcher-update/default.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
makeScript {
writeShellApplication {
name = "nvfetcher-update";
searchPaths.bin = [ nixpkgs.nvfetcher ];
searchPaths.source = [ ];
entrypoint = ./entrypoint.sh;
runtimeInputs = [ nixpkgs.nvfetcher ];
text = nixpkgs.lib.fileContents ./entrypoint.sh;
}
7 changes: 4 additions & 3 deletions units/scripts/nvfetcher-update/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#!/usr/bin/env bash

# Move to the directory of the script

PRJ_ROOT="${PRJ_ROOT:-"$(git rev-parse --show-toplevel)"}"

cd "$(dirname "$PRJ_ROOT/$1")"
# shellcheck disable=all
cd "$(dirname "$PRJ_ROOT/$@")"

# Run nvfetcher with the provided arguments
nvfetcher -c "$PRJ_ROOT/$1" -l /tmp/nvfetcher-changelog
# shellcheck disable=all
nvfetcher -c "$PRJ_ROOT/$@" -l /tmp/nvfetcher-changelog

# If the changelog is not empty and the GITHUB_ENV variable is set
if [[ -s /tmp/nvfetcher-changelog && -n ${GITHUB_ENV:-} ]]; then
Expand Down

0 comments on commit 0b8d60a

Please sign in to comment.