Skip to content

Instantly share code, notes, and snippets.

@jackyliu16
Last active May 26, 2023 15:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jackyliu16/e6952c26a3810e4b46a1d348831984dd to your computer and use it in GitHub Desktop.
Save jackyliu16/e6952c26a3810e4b46a1d348831984dd to your computer and use it in GitHub Desktop.
A flake of nix for rCore
{
description = "A devShell for rCore";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
nixpkgs-qemu7.url = "https://github.com/NixOS/nixpkgs/archive/7cf5ccf1cdb2ba5f08f0ac29fc3d04b0b59a07e4.tar.gz";
rust-overlay.url = "github:oxalica/rust-overlay";
flake-utils.url = "github:numtide/flake-utils";
};
outputs = { self, nixpkgs, nixpkgs-qemu7, rust-overlay, flake-utils, ... }:
flake-utils.lib.eachDefaultSystem (system:
let
overlays = [
(import rust-overlay)
(self: super: {
# ref: https://github.com/the-nix-way/dev-templates
rust-toolchain =
let
rust = super.rust-bin;
in
if builtins.pathExists ./rust-toolchain.toml then
rust.fromRustupToolchainFile ./rust-toolchain.toml
else if builtins.pathExists ./rust-toolchain then
rust.fromRustupToolchainFile ./rust-toolchain
else
# The rust-toolchain when i make this file, which maybe change
(rust.nightly."2022-08-05".minimal.override {
extensions = [ "rust-src" "llvm-tools-preview" "rustfmt" "clippy" ];
targets = [ "riscv64gc-unknown-none-elf" ];
});
qemu = self.callPackage "${nixpkgs-qemu7}/pkgs/applications/virtualization/qemu" {
inherit (self.darwin.apple_sdk.frameworks) CoreServices Cocoa Hypervisor;
inherit (self.darwin.stubs) rez setfile;
inherit (self.darwin) sigtool;
# Reduces the number of qemu source files from ~10000 to ~2600 source files.
hostCpuTargets = ["riscv64-softmmu" "riscv32-softmmu"];
};
})
];
pkgs = import nixpkgs {
inherit system overlays;
};
in
{
devShells.default = pkgs.mkShell {
buildInputs = (with pkgs;[
# Basic
openssl
pkg-config
exa
fd
qemu
# Cross Compile
(with pkgsCross.riscv64; [ musl.stdenv.cc ]) # If use normally, no necessary need to change.
# Rust Configuraiton
rustup
cargo-binutils
rust-toolchain
]);
shellHook = ''
alias ls=exa
alias find=fd
'';
};
}
);
}
@jackyliu16
Copy link
Author

jackyliu16 commented May 24, 2023

I know it's looks strange to download another nixpkgs just for a specify version of qemu, but a backup measure if you have some kinds of error in 8.0.0 (current version of nixpkgs), If you have a better solution, please let me know.

translation:
我知道专门为了qemu7.0.0下载nixpkgs看上去有点奇怪,但是目前我没有更好的方法,如果你有更好的解决方法,请告诉我
这主要是作为一个可能存在的qemu8.0.0不工作的回滚方案。
(如果出错,你需要逐个启用当前被注释掉的qemu相关的内容,并且添加output函数的输入参数以启用qemu7.0.0)

thks for:

@gjz010
Copy link

gjz010 commented May 24, 2023

What about using callPackage to import qemu directly from the old nixpkgs?
Pros: sharing dependencies (e.g. glibc) with other packages in nixpkgs-unstable.
Cons: re-building qemu required for every flake user. Maybe a binary cache (e.g. cachix) can be used, and we can only rebuild riscv32/riscv64 qemu targets.

let qemu7 = self.callPackage "${nixpkgs-qemu7}/pkgs/applications/virtualization/qemu" {
    inherit (self.darwin.apple_sdk.frameworks) CoreServices Cocoa Hypervisor;
    inherit (self.darwin.stubs) rez setfile;
    inherit (self.darwin) sigtool;
    qemu = qemu7;
    # Reduces the number of qemu source files from ~10000 to ~2600 source files.
    hostCpuTargets = ["riscv64-softmmu" "riscv32-softmmu"];
};

@jackyliu16
Copy link
Author

jackyliu16 commented May 25, 2023

What about using callPackage to import qemu directly from the old nixpkgs? Pros: sharing dependencies (e.g. glibc) with other packages in nixpkgs-unstable. Cons: re-building qemu required for every flake user. Maybe a binary cache (e.g. cachix) can be used, and we can only rebuild riscv32/riscv64 qemu targets.

let qemu7 = self.callPackage "${nixpkgs-qemu7}/pkgs/applications/virtualization/qemu" {
    inherit (self.darwin.apple_sdk.frameworks) CoreServices Cocoa Hypervisor;
    inherit (self.darwin.stubs) rez setfile;
    inherit (self.darwin) sigtool;
    qemu = qemu7;
    # Reduces the number of qemu source files from ~10000 to ~2600 source files.
    hostCpuTargets = ["riscv64-softmmu" "riscv32-softmmu"];
};

I guess this diff is what you means. by the ways, what is the reason of inherit (self.darwin.stubs) rez setfile and inherit (self.darwin) sigtool?
Which i have check some code using it, but haven't ideas about this, it's necessary dependence or optimization in macOS?

Sorry about i have no ideas about macOS, but it seems sigTool is using for signature and database management tool, I can't understand the use of this place.

https://stackoverflow.com/questions/69869574/properly-adding-darwin-apple-sdk-to-a-nix-shell

6c6
<     # nixpkgs-qemu7.url = "https://github.com/NixOS/nixpkgs/archive/7cf5ccf1cdb2ba5f08f0ac29fc3d04b0b59a07e4.tar.gz";
---
>     nixpkgs-qemu7.url = "https://github.com/NixOS/nixpkgs/archive/7cf5ccf1cdb2ba5f08f0ac29fc3d04b0b59a07e4.tar.gz";
11c11
<   outputs = { self, nixpkgs, rust-overlay, flake-utils, ... }:
---
>   outputs = { self, nixpkgs, nixpkgs-qemu7, rust-overlay, flake-utils, ... }:
31a32,38
>             qemu7 = self.callPackage "${nixpkgs-qemu7}/pkgs/applications/virtualization/qemu" {
>               inherit (self.darwin.apple_sdk.frameworks) CoreServices Cocoa Hypervisor;
>               inherit (self.darwin.stubs) rez setfile;
>               inherit (self.darwin) sigtool;
>               # Reduces the number of qemu source files from ~10000 to ~2600 source files.
>               hostCpuTargets = ["riscv64-softmmu" "riscv32-softmmu"];
>             };
54,55c61
<             pkgs.qemu
<             # pkg-qemu.qemu
---
>             pkgs.qemu7

@gjz010
Copy link

gjz010 commented May 26, 2023

What about using callPackage to import qemu directly from the old nixpkgs? Pros: sharing dependencies (e.g. glibc) with other packages in nixpkgs-unstable. Cons: re-building qemu required for every flake user. Maybe a binary cache (e.g. cachix) can be used, and we can only rebuild riscv32/riscv64 qemu targets.

let qemu7 = self.callPackage "${nixpkgs-qemu7}/pkgs/applications/virtualization/qemu" {
    inherit (self.darwin.apple_sdk.frameworks) CoreServices Cocoa Hypervisor;
    inherit (self.darwin.stubs) rez setfile;
    inherit (self.darwin) sigtool;
    qemu = qemu7;
    # Reduces the number of qemu source files from ~10000 to ~2600 source files.
    hostCpuTargets = ["riscv64-softmmu" "riscv32-softmmu"];
};

I guess this diff is what you means. by the ways, what is the reason of inherit (self.darwin.stubs) rez setfile and inherit (self.darwin) sigtool? Which i have check some code using it, but haven't ideas about this, it's necessary dependence or optimization in macOS?

Sorry about i have no ideas about macOS, but it seems sigTool is using for signature and database management tool, I can't understand the use of this place.

https://stackoverflow.com/questions/69869574/properly-adding-darwin-apple-sdk-to-a-nix-shell

6c6
<     # nixpkgs-qemu7.url = "https://github.com/NixOS/nixpkgs/archive/7cf5ccf1cdb2ba5f08f0ac29fc3d04b0b59a07e4.tar.gz";
---
>     nixpkgs-qemu7.url = "https://github.com/NixOS/nixpkgs/archive/7cf5ccf1cdb2ba5f08f0ac29fc3d04b0b59a07e4.tar.gz";
11c11
<   outputs = { self, nixpkgs, rust-overlay, flake-utils, ... }:
---
>   outputs = { self, nixpkgs, nixpkgs-qemu7, rust-overlay, flake-utils, ... }:
31a32,38
>             qemu7 = self.callPackage "${nixpkgs-qemu7}/pkgs/applications/virtualization/qemu" {
>               inherit (self.darwin.apple_sdk.frameworks) CoreServices Cocoa Hypervisor;
>               inherit (self.darwin.stubs) rez setfile;
>               inherit (self.darwin) sigtool;
>               # Reduces the number of qemu source files from ~10000 to ~2600 source files.
>               hostCpuTargets = ["riscv64-softmmu" "riscv32-softmmu"];
>             };
54,55c61
<             pkgs.qemu
<             # pkg-qemu.qemu
---
>             pkgs.qemu7

No idea. I just copied these lines from nixpkgs:

https://github.com/NixOS/nixpkgs/blob/7135383939285a8149875c8a19bc012727e9a90d/pkgs/top-level/all-packages.nix#L33649

@jackyliu16
Copy link
Author

jackyliu16 commented May 26, 2023

@gjz010 would you be interested in another one?
Another rust operating system support Multiple Architectur, which flake haven't complete yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment