Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add flake for supporting NixOs and nix systems #110

Merged
merged 2 commits into from
May 30, 2024
Merged
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
3 changes: 3 additions & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
if has nix_direnv_version; then
use flake
fi
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ test*.sh
flamegraph.svg

# MacOS
**/.DS_Store
**/.DS_Store

# Nixos
.direnv
result
19 changes: 19 additions & 0 deletions NIX.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# NixOS and Flakes

The package contains all the required install scripts and dependencies. You can use the flake as:

```nix
{
inputs = {
# ...
amber.url = "github:Ph0enixKM/Amber";
};

# then later with home manager for example
home.packages = [ inputs.amber.packages.${pkgs.system}.default ];
}
```

The package is avaiable as `amber-lang` on [nixpkgs](https://github.com/NixOS/nixpkgs/pull/313774).

While developing with Nix, the flake defines all dependencies for `nix develop` (or `direnv` if used).
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Programming language that compiles to Bash. It's a high level programming langua
Amber compiler currently works on:
- Linux x86 and ARM
- macOS x86 and ARM (Apple Silicon)
- Nix (NixOS)

### macOS / Linux
Make sure that the operating system meets the following prerequisites
Expand All @@ -37,6 +38,10 @@ Amber is packaged in the following distros:

Arch (AUR) - `amber-bash-bin`

#### Nix

See [NIX.md](./NIX.md)

### Windows support
As windows does not come with bash installed it makes no sense to support it. Please install WSL 2 on your windows machine and install Linux version of Amber compiler inside.

Expand Down
95 changes: 95 additions & 0 deletions flake.lock

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

29 changes: 29 additions & 0 deletions flake.nix

Choose a reason for hiding this comment

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

Perhaps we could add an overlay above the nixpkgs package amber-lang

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
inputs = {
naersk.url = "github:nix-community/naersk/master";
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
utils.url = "github:numtide/flake-utils";
Comment on lines +3 to +5

Choose a reason for hiding this comment

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

I don't see much point in adding flake-utils or naersk. But it's fine, we can certainly add them.

};

outputs = { self, nixpkgs, utils, naersk }:
utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs { inherit system; };
naersk-lib = pkgs.callPackage naersk { };
in
{
packages.default = naersk-lib.buildPackage {
src = ./.;
postInsall = ''
wrapProgram "$out/bin/amber" --set PATH ${nixpkgs.lib.makeBinPath [
pkgs.bc
]}
'';
};
devShells.default = with pkgs; mkShell {
buildInputs = [ bc cargo rustc rustfmt pre-commit rustPackages.clippy ];
RUST_SRC_PATH = rustPlatform.rustLibSrc;
};
}
);
}
6 changes: 3 additions & 3 deletions src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,13 @@ impl AmberCompiler {

pub fn execute(code: String, flags: &[String]) {
let code = format!("set -- {};\n\n{}", flags.join(" "), code);
Command::new("/bin/bash").arg("-c").arg(code).spawn().unwrap().wait().unwrap();
Command::new("/usr/bin/env").arg("bash").arg("-c").arg(code).spawn().unwrap().wait().unwrap();
brumik marked this conversation as resolved.
Show resolved Hide resolved
}

#[allow(dead_code)]
pub fn test_eval(&mut self) -> Result<String, Message> {
self.compile().map_or_else(Err, |(_, code)| {
let child = Command::new("/bin/bash")
let child = Command::new("/usr/bin/env").arg("bash")
.arg("-c").arg::<&str>(code.as_ref())
.output().unwrap();
Ok(String::from_utf8_lossy(&child.stdout).to_string())
Expand All @@ -137,4 +137,4 @@ impl AmberCompiler {
include_str!("std/main.ab")
].join("\n")
}
}
}