Giter Site home page Giter Site logo

nix-deno's Introduction

Logo

nix-deno

Nix builders for Deno projects.

Still a WIP, but here's what works / is done:

  • denoPlatform.mkDenoDir: cache Deno dependencies in a pure Nix expression, no external scripts needed.
  • scripts without dependencies.
  • https://deno.land, https://esm.sh style dependencies.
  • NPM dependencies. I use this repo to build my Lume website, which has lots of NPM dependencies, please open an issue if you run into NPM problems!
  • denoPlatform.mkDenoDerivation: a basic stdenv.mkDerivation wrapper that handles dependencies for you.
  • helper for deno compile.
  • helper for deno bundle. Newer alternatives like deno_emit are still a TODO.
  • more helpful docs, right now, I'd suggest having a look at ./ci.nix for basic usage info.
  • a release.

Basic Usage

This section will guide you through setting up and building a simple Deno application using the nix-deno overlay.

The examples assume the following structure:

Deno Project
โ”‚
โ”œโ”€โ”€ main.ts       # The main TypeScript file for your Deno application
โ”œโ”€โ”€ deno.json     # The Deno project configuration file
โ”œโ”€โ”€ deno.lock     # A lock file for managing dependencies
โ””โ”€โ”€ flake.nix     # The flake under discussion below

Building an Executable

Here's a complete flake.nix example for building an executable for a typical Deno project:

{
  inputs.nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
  inputs.flake-utils.url = "github:numtide/flake-utils";
  inputs.nix-deno.url = "github:nekowinston/nix-deno";

  outputs = { self, nixpkgs, flake-utils, ... } @ inputs:
    flake-utils.lib.eachDefaultSystem (system: let
      pkgs = import nixpkgs {
        inherit system;
        overlays = [ inputs.nix-deno.overlays.default ];
      };
    in {
      packages.example-executable = pkgs.denoPlatform.mkDenoBinary {
        name = "example-executable";
        version = "0.1.2";
        src = ./.;
        buildInputs = [ ]; # other dependencies
        permissions.allow.all = true;
      };
      defaultPackage = self.packages.${system}.example-executable;
    });
}

Generating Artifacts

For projects that generate artifacts (in this example a deno app which generates a folder of pdf files), you might have a deno.json like:

{
  "tasks": {
    "buildPdfs": "deno run --allow-run --allow-net --allow-read=/ --allow-write=. main.ts"
  }
}

In which case your flake.nix might look like this:

{
  inputs.nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
  inputs.flake-utils.url = "github:numtide/flake-utils";
  inputs.nix-deno.url = "github:nekowinston/nix-deno";

  outputs = { self, nixpkgs, flake-utils, ... } @ inputs:
    flake-utils.lib.eachDefaultSystem (system: let
      pkgs = import nixpkgs {
        inherit system;
        overlays = [ inputs.nix-deno.overlays.default ];
      };
    in {
      packages.pdfGen = pkgs.denoPlatform.mkDenoDerivation {
        name = "pdfGen";
        version = "0.1.2";
        src = ./.;
        buildInputs = [ pkgs.xdg-utils ]; # assuming reliance on xdg-utils
        buildPhase = ''
          mkdir -p $out
          deno task buildPdfs
          '';
        installPhase = ''
          cp ./*.pdf $out
        '';
      };
      defaultPackage = self.packages.${system}.pdfGen;
    });
}

Building and Running Your Project

Build the Project: Execute nix build in your project directory. This will build the project based on the flake.nix configuration.

Run the Executable or Access Artifacts:

If you do not override the buildPhase (like we have not, in the example-executable), after building you can run ./result/bin/example-executable.

The example with the pdfGen overrides the buildPhase and installPhase, so you will only see whatever is copied into the $out folder manually (some pdfs in this case).

License

MIT


I love Deno. ๐Ÿฅฐ

nix-deno's People

Contributors

nekowinston avatar meatcar avatar vtgen avatar

Stargazers

Peter Rice avatar didier amyot avatar Marc Jakobi avatar Tak avatar hasundue avatar Lorenzo Good avatar Sam Bossley avatar Isabel avatar seth avatar  avatar  avatar

Watchers

didier amyot avatar  avatar

nix-deno's Issues

NPM: Same dependency, different versions causes failure

If there are two NPM dependencies in the deno.lock with varying dependencies, such as

import color from "npm:[email protected]";
import "npm:[email protected]";

The build fails. That's because the registry.json symlink that's created last takes precedence. To fix it, I'll have to combine the info of both cached packages.
That will probably have to get handled in the denoCacheRestoreHook, and removed from the mkDenoDir expression.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.