Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use_flake
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@
*.pcap

# docship
/output
/output

.direnv/
61 changes: 61 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 54 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not familiar with Nix and is there a way to simplify the change? I'd keep the reference implementation simple, for example if we can do this in an one-liner (like we do apt install), it would be great for newbies.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can be make shorter because I added some boilerplate to make nix shell functional too, although it can't be turned into an one-liner.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for sharing. Let me get started with Nix first later and try this 😄

Copy link
Contributor

@FjolleJagt FjolleJagt Feb 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For what it's worth, if you want a flake.nix that also builds the kernel:

description = "build environment";

inputs.nixpkgs.url = "github:NixOS/nixpkgs/24.05";

inputs.flake-utils.url = "github:numtide/flake-utils";

outputs =
{ self
, nixpkgs
, flake-utils
,
}:
flake-utils.lib.eachDefaultSystem (
system:
let
pkgs = nixpkgs.legacyPackages.${system};
in
{
packages = {
default = pkgs.stdenv.mkDerivation {
version = "1.0.0";
pname = "dev env";
src = ./.;

nativeBuildInputs = with pkgs.buildPackages; [
llvmPackages.clang-unwrapped
llvmPackages.bintools-unwrapped
];

# first path will be computed within build env, the second one will be taken from the environment
installPhase = ''
mkdir -p $out/bin
{ echo "export PATH=$PATH:\$PATH";
echo "exec \$SHELL";
} >> $out/bin/dev-env

chmod +x $out/bin/dev-env
'';
};
};

devShells.default = pkgs.mkShell {
nativeBuildInputs = with pkgs.buildPackages; [
llvmPackages.clang-unwrapped
llvmPackages.bintools-unwrapped
];

shellHook = ''
'';
};
}
);
}
25 changes: 22 additions & 3 deletions run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,32 @@
set -xue

QEMU=qemu-system-riscv32
CC=/opt/homebrew/opt/llvm/bin/clang
OBJCOPY=/opt/homebrew/opt/llvm/bin/llvm-objcopy

CFLAGS="-std=c11 -O2 -g3 -Wall -Wextra --target=riscv32 -ffreestanding -nostdlib"
if [[ -n "$IN_NIX_SHELL" ]]; then
CC=clang
OBJCOPY=llvm-objcopy
else
TOOLCHAIN="${TOOLCHAIN:-}"
if [[ ! -f $TOOLCHAIN/clang ]]; then
if [[ -f /run/current-system/sw/bin/clang ]]; then
TOOLCHAIN=/run/current-system/sw/bin
elif [[ -f /opt/homebrew/opt/llvm/bin/clang ]]; then
TOOLCHAIN=/opt/homebrew/opt/llvm/bin/
else
echo "Cannot find toolchain"
exit 1
fi
fi
echo "Using toolchain at ${TOOLCHAIN}"
CC=$TOOLCHAIN/clang
OBJCOPY=$TOOLCHAIN/llvm-objcopy
fi

CFLAGS="-std=c11 -O2 -gdwarf-4 -g3 -Wall -Wextra --target=riscv32-unknown-elf -fno-stack-protector -ffreestanding -nostdlib"

# シェルをビルド
$CC $CFLAGS -Wl,-Tuser.ld -Wl,-Map=shell.map -o shell.elf shell.c user.c common.c

$OBJCOPY --set-section-flags .bss=alloc,contents -O binary shell.elf shell.bin
$OBJCOPY -Ibinary -Oelf32-littleriscv shell.bin shell.bin.o

Expand Down