Skip to content

Commit

Permalink
sd-image-qemu-sway: init
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaofengli committed Jan 16, 2022
1 parent 106610b commit f442457
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 23 deletions.
8 changes: 7 additions & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@
outputs = { self, nixpkgs }: let
pkgs = import nixpkgs {
system = "riscv64-linux";
overlays = [ self.overlay ];
overlays = [
(final: prev: {
nixos-riscv64 = self.outPath;
})

self.overlay
];
};
in {
overlay = import ./pkgs;
Expand Down
1 change: 1 addition & 0 deletions pkgs/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ final: prev: rec {

# QEMU
sd-image-qemu = final.callPackage ./sd-image-qemu { };
sd-image-qemu-sway = final.callPackage ./sd-image-qemu-sway { };

# HiFive Unmatched
linux_unmatched = final.callPackage ./linux { };
Expand Down
27 changes: 7 additions & 20 deletions pkgs/sd-image-qemu-sway/configuation.nix
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
{ config, pkgs, modulesPath, ... }:
let
mesa-patched = pkgs.mesa.overrideAttrs (old: {
patches = (old.patches or []) ++ [
./gallivm-riscv.patch
];
});
in {
{
imports = [
(modulesPath + "/installer/sd-card/sd-image-riscv64-qemu.nix")
];

nixpkgs.overlays = [
(import <nixos-riscv64/pkgs>)
];

hardware.pulseaudio.enable = true;

services.xserver = {
enable = true;


# For OOBE - You may want to disable the following
displayManager.autoLogin = {
enable = true;
Expand All @@ -37,21 +34,11 @@ in {
};

environment.systemPackages = with pkgs; [
unmatched.firefox
riscv64.firefox
superTuxKart
glxinfo
kitty
];

system.replaceRuntimeDependencies = [
{
original = pkgs.mesa;
replacement = mesa-patched;
}
{
original = pkgs.mesa.drivers;
replacement = mesa-patched.drivers;
}
pavucontrol
];

# imake/xorg-cf-files doesn't have riscv64 definitions merged
Expand Down
11 changes: 9 additions & 2 deletions pkgs/sd-image-qemu-sway/default.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ lib, nixos, nix-gitignore, runCommandLocal
{ lib, nixos, nix-gitignore, runCommandLocal, ubootQemuRiscv64Smode
, nixos-riscv64 ? nix-gitignore.gitignoreSource [] ../.. }:

let
Expand All @@ -7,12 +7,14 @@ let
cp -prd ${nixos-riscv64} $out/nixos-riscv64
'';

config = { pkgs, modulesPath, config, ... }: {
config = { lib, pkgs, modulesPath, config, ... }: {
imports = [
(modulesPath + "/installer/sd-card/sd-image-riscv64-qemu-installer.nix")
./configuation.nix
];

nixpkgs.overlays = lib.mkForce [];

boot.postBootCommands = lib.mkAfter ''
# Copy nixos-riscv64 channel
if ! [ -e /var/lib/nixos/did-nixos-riscv64-channel-init ]; then
Expand All @@ -29,6 +31,11 @@ let
cp ${./configuation.nix} /etc/nixos/configuration.nix
fi
'';

sdImage.postBuildCommands = ''
cp ${ubootQemuRiscv64Smode}/u-boot.bin $out/
cp ${./launch.sh} $out/launch.sh
'';
};
eval = nixos config;
in eval.config.system.build.sdImage
40 changes: 40 additions & 0 deletions pkgs/sd-image-qemu-sway/launch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env bash

set -euo pipefail

BASE=$(dirname $0)

QEMU=qemu-system-riscv64
USERDISK=nixos-riscv64-qemu.raw
ORIGDISK=$(ls $BASE/sd-image/*.zst)
UBOOT=$(ls $BASE/u-boot.bin)

check_command() {
if ! command -v $1 &>/dev/null 2>&1; then
>&2 echo "$1 is required to run this. Try \"nix-shell -p qemu -p zstd\"."
exit 1
fi
}

check_command $QEMU
check_command qemu-img
check_command zstdcat

if [ ! -e $USERDISK ]; then
>&2 echo "Creating a persistent disk image at $USERDISK..."
zstdcat $ORIGDISK > $USERDISK
fi

>&2 echo "Launching QEMU..."

set -x
exec $QEMU -M virt -cpu rv64 -m 2G -smp 4 \
-kernel $UBOOT \
-display gtk,gl=on \
-device qemu-xhci \
-device virtio-vga-gl -device usb-tablet -device virtio-keyboard \
-device intel-hda -device hda-duplex,audiodev=pa -audiodev pa,id=pa \
-device virtio-blk-device,drive=sdimage \
-drive file=$USERDISK,format=raw,id=sdimage \
-device virtio-net-device,netdev=usernet \
-netdev user,id=usernet,net=10.0.0.0/24,dhcpstart=10.0.0.10

0 comments on commit f442457

Please sign in to comment.