Skip to content

Commit

Permalink
[nix] flake
Browse files Browse the repository at this point in the history
Co-authored-by: John Boehr <[email protected]>
Co-authored-by: Rahul Butani <[email protected]>
  • Loading branch information
jbboehr and rrbutani committed Feb 15, 2023
1 parent a993008 commit 3a5eb29
Show file tree
Hide file tree
Showing 29 changed files with 1,231 additions and 114 deletions.
2 changes: 1 addition & 1 deletion .envrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# use flake
use flake
93 changes: 88 additions & 5 deletions .github/workflows/common.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ jobs:
format:
name: Format
runs-on: ubuntu-22.04
if: "!contains(github.event.head_commit.message, '[skip format]')"
steps:
- name: Checkout
uses: actions/checkout@v3
Expand Down Expand Up @@ -61,9 +62,11 @@ jobs:
working-directory: ${{ inputs.workdir }}
shell: bash
run: ./format.sh && git diff --exit-code

build:
name: Build
runs-on: ubuntu-22.04
if: "!contains(github.event.head_commit.message, '[skip build]')"
steps:
- name: Checkout
uses: actions/checkout@v3
Expand Down Expand Up @@ -103,26 +106,106 @@ jobs:
- name: Benchmark Test
shell: bash
run: ./utils/bench.py ${{ inputs.workdir }} --default --frames 10

nix:
name: Nix
runs-on: ubuntu-22.04
if: ${{ !contains(github.event.head_commit.message, '[skip nix]') }}
outputs:
flake_status: ${{ steps.early.outputs.flake_status }}
shell_status: ${{ steps.early.outputs.shell_status }}
steps:
- name: Checkout
uses: actions/checkout@v3
- id: early
name: Check if nix files exist
shell: bash
working-directory: ${{ inputs.workdir }}
run: |
flake_status="skip"
if [ -f "derivation.nix" ]; then
flake_status="found"
fi
echo "flake_status=${flake_status}" >> "${GITHUB_OUTPUT}"
shell_status="skip"
if [ -f "shell.nix" ]; then
shell_status="found"
fi
echo "shell_status=${shell_status}" >> "${GITHUB_OUTPUT}"
nix-flake:
name: Nix Flake
runs-on: ubuntu-22.04
needs: nix
if: ${{ !contains(github.event.head_commit.message, '[skip nix flake]') && needs.nix.outputs.flake_status == 'found' }}
steps:
- name: Checkout
uses: actions/checkout@v3
with:
submodules: recursive
- name: Set Up Build Cache
uses: actions/cache@v3
id: nix-cache
with:
path: |
/nix
key: ${{ inputs.workdir }}-nix-${{ hashFiles(format('{0}/shell.nix', inputs.workdir)) }}
restore-keys: ${{ inputs.workdir }}-nix-
path: /tmp/nix-store.nar
key: ${{ runner.os }}-${{ inputs.workdir }}-nix-store.nar-${{ hashFiles('flake.nix', 'flake.lock', format('{0}/derivation.nix', inputs.workdir)) }}
restore-keys: ${{ runner.os }}-${{ inputs.workdir }}-nix-store.nar-
- name: Install Deps
uses: cachix/install-nix-action@v15
with:
nix_path: nixpkgs=channel:nixos-22.11
- name: Run
# see: https://github.com/cachix/install-nix-action/issues/56#issuecomment-1300421537
- name: "Import Nix Store Cache"
if: "steps.nix-cache.outputs.cache-hit == 'true'"
run: "nix-store --import < /tmp/nix-store.nar"
- name: Flake Build
shell: bash
run: |
nix eval .#packages --impure --raw \
--apply '(import ./utils/get-builds-for-lang.nix { lang = "${{ inputs.workdir }}"; attr = "packages"; })' \
| xargs nix build -L
- name: Flake Run
shell: bash
run: |
nix eval .#packages --impure --raw \
--apply '(import ./utils/get-builds-for-lang.nix { lang = "${{ inputs.workdir }}"; prefix = ".#"; })' \
| xargs -I{} nix run {} -- --help
- name: Flake Shell
shell: bash
working-directory: ${{ inputs.workdir }}
run: nix develop ..#${{ inputs.workdir }} --command bash -c './build.sh && ./rosettaboy-release --help'
- name: Flake Checks
shell: bash
run: |
nix eval .#checks --impure --raw \
--apply '(import ./utils/get-builds-for-lang.nix { lang = "${{ inputs.workdir }}"; attr = "checks"; })' \
| xargs nix build -L
- name: "Export Nix Store Cache"
shell: bash
# we could add devShells here but it might fill the cache fast...
run: |
nix eval .#packages --impure --raw \
--apply '(import ./utils/get-builds-for-lang.nix { lang = "${{ inputs.workdir }}"; attr = "packages"; })' \
| xargs -I{} bash -c "nix eval --raw {}; echo" \
| xargs nix-store -qR \
| xargs nix-store --export > /tmp/nix-store.nar
nix-shell:
name: Nix Shell
runs-on: ubuntu-22.04
needs: nix
if: ${{ !contains(github.event.head_commit.message, '[skip nix flake]') && needs.nix.outputs.shell_status == 'found' }}
steps:
- name: Checkout
uses: actions/checkout@v3
with:
submodules: recursive
- name: Install Nix
uses: cachix/install-nix-action@v15
with:
nix_path: nixpkgs=channel:nixos-22.11
- name: Run Shell
shell: bash
working-directory: ${{ inputs.workdir }}
run: nix-shell --pure --run './build.sh && ./rosettaboy-release --help'
2 changes: 1 addition & 1 deletion .github/workflows/nim.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ jobs:
with:
workdir: nim
with-nim: stable
build-pkgs: libsdl2-dev
build-pkgs: libsdl2-dev
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ If you have either Nix or Docker available, you can run `./utils/shell.sh` to cr

If you prefer Docker, you can use `./utils/shell-docker.sh` instead.

If you prefer Nix, you can manually run `nix-shell` instead. When run in an implemention's directory, `nix-shell` will only provide what is needed for that language, and when run in the project root it will provide everything needed for all languages.
If you prefer Nix, you can manually run `nix develop` or `nix-shell` instead. When run with an implementation as an argument, e.g. `nix develop .#py`, it will only provide what is needed for that language, and when run in the project root it will provide everything needed for all languages. Alternatively, there is also an integration with [nix-direnv](https://github.com/nix-community/nix-direnv).


Benchmarks
Expand Down
1 change: 1 addition & 0 deletions c/.envrc
33 changes: 0 additions & 33 deletions c/default.nix

This file was deleted.

43 changes: 23 additions & 20 deletions c/derivation.nix
Original file line number Diff line number Diff line change
@@ -1,41 +1,44 @@
{ lib
, stdenv
, cmake
, SDL2
, autoPatchelfHook
, pkg-config
, valgrind ? null
, clang-tools ? null
, nixpkgs-fmt ? null
, ltoSupport ? false
, debugSupport ? false
{
lib,
stdenv,
cmake,
SDL2,
pkg-config,
cleanSource,
clang-tools ? null,
ltoSupport ? false,
debugSupport ? false
}:

let
devTools = [ clang-tools nixpkgs-fmt valgrind ];
devTools = [ clang-tools ];
in

stdenv.mkDerivation {
stdenv.mkDerivation rec {
name = "rosettaboy-c";

src = ./.;
src = cleanSource {
inherit name;
src = ./.;
extraRules = ''
.clang-format
'';
};

passthru = { inherit devTools; };

enableParallelBuilding = true;

buildInputs = [ SDL2 ];
nativeBuildInputs = [ autoPatchelfHook cmake pkg-config ]
++ lib.optionals debugSupport devTools;
nativeBuildInputs = [ cmake pkg-config ];

cmakeFlags = [ ]
++ lib.optional debugSupport "-DCMAKE_BUILD_TYPE=Debug"
++ lib.optional (!debugSupport) "-DCMAKE_BUILD_TYPE=Release"
++ lib.optional ltoSupport "-DENABLE_LTO=On"
;

meta = with lib; {
description = "rosettaboy-c";
mainProgram = "rosettaboy-c";
meta = {
description = name;
mainProgram = name;
};
}
10 changes: 0 additions & 10 deletions c/shell.nix

This file was deleted.

1 change: 1 addition & 0 deletions cpp/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ CMakeFiles
Makefile
cmake_install.cmake
rosettaboy-cpp
/result*
4 changes: 4 additions & 0 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ option(ENABLE_LTO "enable LTO" OFF)
include(CheckIPOSupported)
check_ipo_supported(RESULT supported OUTPUT error)

include(GNUInstallDirs)

include_directories(/usr/local/include/)

add_executable(rosettaboy-cpp src/main.cpp src/args.cpp src/cpu.cpp src/cart.cpp src/gameboy.cpp src/gpu.cpp src/consts.h src/cart.h src/cpu.h src/gpu.h src/args.h src/apu.cpp src/apu.h src/ram.cpp src/ram.h src/buttons.cpp src/buttons.h src/clock.cpp src/clock.h)
install(TARGETS rosettaboy-cpp
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})

if( ENABLE_LTO AND supported )
message(STATUS "IPO / LTO enabled")
Expand Down
41 changes: 41 additions & 0 deletions cpp/derivation.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
lib,
stdenv,
cmake,
SDL2,
fmt_8,
pkg-config,
cleanSource,
clang-format ? null,
ltoSupport ? false,
debugSupport ? false,
}:

stdenv.mkDerivation rec {
name = "rosettaboy-cpp";
src = cleanSource {
inherit name;
src = ./.;
extraRules = ''
.clang-format
'';
};

buildInputs = [ SDL2 fmt_8 ];
nativeBuildInputs = [ cmake pkg-config ];

passthru = {
devTools = [ clang-format ];
};

cmakeFlags = [ ]
++ lib.optional debugSupport "-DCMAKE_BUILD_TYPE=Debug"
++ lib.optional (!debugSupport) "-DCMAKE_BUILD_TYPE=Release"
++ lib.optional ltoSupport "-DENABLE_LTO=On"
;

meta = {
description = name;
mainProgram = name;
};
}
10 changes: 10 additions & 0 deletions default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
(import
(
let lock = builtins.fromJSON (builtins.readFile ./flake.lock); in
fetchTarball {
url = "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz";
sha256 = lock.nodes.flake-compat.locked.narHash;
}
)
{ src = ./.; }
).defaultNix
Loading

0 comments on commit 3a5eb29

Please sign in to comment.