Skip to content

Commit

Permalink
Add abstraction for creating apps in flake
Browse files Browse the repository at this point in the history
  • Loading branch information
jacg committed Nov 27, 2023
2 parents 93dea49 + 6914515 commit eb34971
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 73 deletions.
102 changes: 102 additions & 0 deletions .github/workflows/bootstrap.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
name: Bootstrap and test new nain4 client project
on:
pull_request:
push:
paths-ignore:
- README.md
- .gitignore
- doc
- docs

jobs:
build-and-test:

# See https://wildwolf.name/github-actions-how-to-avoid-running-the-same-workflow-multiple-times/
if: (! contains(github.event.head_commit.message, '[skip ci]') &&
((github.event_name != 'pull_request') ||
(github.event.pull_request.head.repo.full_name !=
github.event.pull_request.base.repo.full_name))
)

runs-on: ${{ matrix.os }}
#continue-on-error: ${{ matrix.allow-fail }}
#continue-on-error: ${{ matrix.devshell == 'gcc' }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-22.04, macos-12 ]
py: [311]
devshell: [ clang, gcc ]
#allow-fail: [false]

exclude:
- os: macos-12
devshell: gcc

steps:
- uses: actions/[email protected]
with:
ref: ${{ github.event.pull_request.head.sha }}
- uses: cachix/install-nix-action@v22
with:
nix_path: nixpkgs=channel:nixos-23.05
extra_nix_config: |
experimental-features = nix-command flakes
- uses: cachix/cachix-action@v12
with:
name: nain4 # The name of the Cachix cache
# If you chose signing key for write access
#signingKey: '${{ secrets.CACHIX_SIGNING_KEY }}'
# If you chose API tokens for write access OR if you have a private cache
authToken: '${{ secrets.CACHIX_JACG_NAIN4 }}'

- name: Test bootstrapping of client project
run: |
cd ~/work
git config --global user.name Tester
git config --global user.email [email protected]
nix run github:jacg/nain4#bootstrap-client-project my-project name-chosen-by-me "description of my project"
cd my-project
nix develop -c just | tee just-output
PATTERN="end of event 10"
echo Looking for "'$PATTERN'" in output of 'just'
grep "$PATTERN" just-output
- name: Test nix run client app
run: |
nix run ~/work/my-project# -- -n 13 --early "/my/bubble_radius 0.2 m" | tee run-client-app-output
PATTERN="end of event 13"
echo Looking for "'$PATTERN'" in output of 'just'
grep "$PATTERN" run-client-app-output
- name: Test nix build client package
run: |
nix build ~/work/my-project#
- name: Test run nix build client package result
run: |
nix develop ~/work/my-project# -c result/bin/name-chosen-by-me --beam-on 12 | tee build-and-run-client-package-output
PATTERN="end of event 12"
echo Looking for "'$PATTERN'" in output of 'just'
grep "$PATTERN" build-and-run-client-package-output
continue-on-error: true # see issue # https://github.com/jacg/nain4/issues/161

- name: Test variables set by nain4 setupHook
run: |
nix develop ~/work/my-project# -c env > my-project-env
echo ====== grepping for G4_DIR ====================
grep G4_DIR my-project-env
echo ===============================================
echo ====== grepping for G4_EXAMPLES ===============
grep G4_EXAMPLES my-project-env
echo ===============================================
echo grepping for QT_QPA_PLATFORM_PLUGIN_PATH
grep QT_QPA_PLATFORM_PLUGIN_PATH my-project-env
echo ===============================================
- name: Check that mdbook ANCHORs have been stripped from bootstrapped project
run: |
nix profile install nixpkgs#ripgrep
echo LOOKING FOR ANY REMAINING ANCHORS
rg ANCHOR ~/work/my-project | tee remaining-anchors
! test -s remaining-anchors
58 changes: 11 additions & 47 deletions .github/workflows/test.yml → .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ jobs:

# See https://wildwolf.name/github-actions-how-to-avoid-running-the-same-workflow-multiple-times/
if: (! contains(github.event.head_commit.message, '[skip ci]') &&
! contains(github.event.head_commit.message, '[skip nain4]') &&
((github.event_name != 'pull_request') ||
(github.event.pull_request.head.repo.full_name !=
github.event.pull_request.base.repo.full_name))
Expand Down Expand Up @@ -62,64 +63,26 @@ jobs:
- name: Test nain4
run: nix develop .#${{ matrix.devshell }} -c just test-nain4 -v

- name: Test bootstrapping of client project
run: |
cd ~/work
git config --global user.name Tester
git config --global user.email [email protected]
nix run github:jacg/nain4#bootstrap-client-project my-project name-chosen-by-me "description of my project"
cd my-project
nix develop -c just | tee just-output
PATTERN="end of event 10"
echo Looking for "'$PATTERN'" in output of 'just'
grep "$PATTERN" just-output
- name: Check that mdbook ANCHORs have been stripped from bootstrapped project
run: |
nix profile install nixpkgs#ripgrep
echo LOOKING FOR ANY REMAINING ANCHORS
rg ANCHOR ~/work/my-project | tee remaining-anchors
! test -s remaining-anchors
- name: Test nix build client package
run: |
nix build ~/work/my-project#
- name: Run client-side tests
run: nix develop .#${{ matrix.devshell }} -c just test-client-side

- name: Test run nix build client package result
run: |
nix develop ~/work/my-project# -c result/bin/name-chosen-by-me --beam-on 12 | tee build-and-run-client-package-output
PATTERN="end of event 12"
echo Looking for "'$PATTERN'" in output of 'just'
grep "$PATTERN" build-and-run-client-package-output
continue-on-error: true # see issue # https://github.com/jacg/nain4/issues/161
- name: Run compile-time tests
run: nix develop .#${{ matrix.devshell }} -c just test-compile-time
continue-on-error: ${{ matrix.os == 'macos-12' }} # bizarre time-dependent encoding errors in python test generator

- name: Test variables set by nain4 setupHook
run: |
nix develop ~/work/my-project# -c env > my-project-env
nix develop .# -c env > nain4-project-env
echo ====== grepping for G4_DIR ====================
grep G4_DIR my-project-env
grep G4_DIR nain4-project-env
echo ===============================================
echo ====== grepping for G4_EXAMPLES ===============
grep G4_EXAMPLES my-project-env
grep G4_EXAMPLES nain4-project-env
echo ===============================================
echo grepping for QT_QPA_PLATFORM_PLUGIN_PATH
grep QT_QPA_PLATFORM_PLUGIN_PATH my-project-env
grep QT_QPA_PLATFORM_PLUGIN_PATH nain4-project-env
echo ===============================================
- name: Test nix run client app
run: |
nix run ~/work/my-project# -- -n 13 --early "/my/bubble_radius 0.2 m" | tee run-client-app-output
PATTERN="end of event 13"
echo Looking for "'$PATTERN'" in output of 'just'
grep "$PATTERN" run-client-app-output
- name: Run client-side tests
run: nix develop .#${{ matrix.devshell }} -c just test-client-side

- name: Run compile-time tests
run: nix develop .#${{ matrix.devshell }} -c just test-compile-time
continue-on-error: ${{ matrix.os == 'macos-12' }} # bizarre time-dependent encoding errors in python test generator

- name: Run N4 examples
run: nix develop .#${{ matrix.devshell }} -c just n4-examples/run-all

Expand All @@ -131,6 +94,7 @@ jobs:

- name: Check for presence of debug symbols in Geant4
run: |
nix profile install nixpkgs#ripgrep
if ${{ matrix.os == 'macos-12' }}
then
if rg --files-with-matches debug_info result/lib/libG4*.so
Expand Down
1 change: 0 additions & 1 deletion flake/outputs.nix
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@
REPLACE "CHANGEME-TESTS-PROJECT-NAME" ''${BASE_NAME}-tests
REPLACE "CHANGEME-PROJECT-TEST-EXE" ''${BASE_NAME}-test
REPLACE "CHANGEME-PACKAGE" ''${BASE_NAME}
REPLACE "CHANGEME-APP" ''${BASE_NAME}
REPLACE "CHANGEME-ONE-LINE-PROJECT-DESCRIPTION" "''${DESCRIPTION}"
git -c init.defaultBranch=master init -q
Expand Down
55 changes: 30 additions & 25 deletions templates/basic/flake/outputs.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,12 @@
}: let
inherit (nixpkgs.legacyPackages) pkgs;

in {

packages.default = self.packages.CHANGEME-PACKAGE;

# Executed by `nix run <URL of this flake>#CHANGEME-PACKAGE -- <args?>`
# TODO: switch to clang environment
packages.CHANGEME-PACKAGE = pkgs.stdenv.mkDerivation {
pname = "CHANGEME-PACKAGE";
version = "0.0.0";
src = "${self}/src";
nativeBuildInputs = [];
buildInputs = [ nain4.packages.nain4 ];
};

# Executed by `nix run <URL of this flake> -- <args?>`
apps.default = self.apps.CHANGEME-APP;

# Executed by `nix run <URL of this flake>#CHANGEME-APP`
apps.CHANGEME-APP = let
# Utility for making Nix flake apps. A nix flake app allows "remote" execution of pre-packaged code.
make-app = executable: args:
let
g4-data = nain4.deps.g4-data-package;
CHANGEME-APP-app-package = pkgs.writeShellScriptBin "CHANGEME-APP" ''
export PATH=${pkgs.lib.makeBinPath [ self.packages.CHANGEME-PACKAGE ]}:$PATH
crystal-app-package = pkgs.writeShellScriptBin executable ''
export PATH=${pkgs.lib.makeBinPath [ self.packages.crystal ]}:$PATH
# export LD_LIBRARY_PATH=${pkgs.lib.makeLibraryPath [ nain4.packages.geant4 ] }:$LD_LIBRARY_PATH
# TODO replace manual envvar setting with with use of packages' setupHooks
Expand All @@ -42,10 +26,31 @@
export G4INCLDATA="${g4-data.G4INCL}/share/Geant4-11.0.4/data/G4INCL1.0"
export G4ENSDFSTATEDATA="${g4-data.G4ENSDFSTATE}/share/Geant4-11.0.4/data/G4ENSDFSTATE2.3"
exec CHANGEME-EXE --macro-path ${self}/macs "$@"
exec ${executable} ${args}
'';
in { type = "app"; program = "${CHANGEME-APP-app-package}/bin/CHANGEME-APP"; };
in { type = "app"; program = "${crystal-app-package}/bin/${executable}"; };

args-from-cli = ''"$@"'';

in {

packages.default = self.packages.CHANGEME-PACKAGE;

# Executed by `nix run <URL of this flake>#CHANGEME-PACKAGE -- <args?>`
# TODO: switch to clang environment
packages.CHANGEME-PACKAGE = pkgs.stdenv.mkDerivation {
pname = "CHANGEME-PACKAGE";
version = "0.0.0";
src = "${self}/src";
nativeBuildInputs = [];
buildInputs = [ nain4.packages.nain4 ];
};

# Executed by `nix run <URL of this flake> -- <args?>`
apps.default = self.apps.CHANGEME-EXE;

# Executed by `nix run <URL of this flake>#CHANGEME-EXE`
apps.CHANGEME-APP-NEW = make-app "CHANGEME-EXE";

# Used by `direnv` when entering this directory (also by `nix develop <URL to this flake>`)
devShell = self.devShells.clang;
Expand All @@ -67,8 +72,8 @@
# 3. [on lxplus] `singularity run hello.img`
packages.singularity = pkgs.singularity-tools.buildImage {
name = "CHANGEME-PROJECT-NAME";
contents = [ self.apps.CHANGEME-APP.program ];
runScript = "${self.apps.CHANGEME-APP.program} $@";
contents = [ self.apps.CHANGEME-EXE.program ];
runScript = "${self.apps.CHANGEME-EXE.program} $@";
diskSize = 10240;
memSize = 5120;
};
Expand Down

0 comments on commit eb34971

Please sign in to comment.