module in F# interactive startup file - f#

I have a finit.fsx file to be load at fsi.exe startup like:
#r xxxx
type xx = xxx
module Util =
let cd path = xxxx
...
After start fsi.exe, it is loaded as
namespace FSI_0002
module Util = begin
val cd : string -> unit
...
end
The problem is how can I use module util? I cannot open Util or use cd directly.
To further puzzle me, if I put a namespace at the top line like namespace Test, In fsi.exe, it is loaded as
namespace FSI_0002.Test
val cd : string -> unit
...
Where is the Module Util? I then have to open Test;; then Util.cd.
Is there a way to define module in the F# startup script and auto open the module? thanks.

I believe you can explicitly specify that all code in the .fsx file is in some module (say, Main) and add the AutoOpen attribute to the module:
[<AutoOpen>]
module Main
let foo = 10
When you then #load the file, the foo value should be visible at the top-level.

[<AutoOpen>]
module Util =
let cd path = ...

Related

what is ${modulesPath} is configuration.nix

In /etc/nixos/configuration.nix, I have this code
{ lib, pkgs, config, modulesPath, ... }:
with lib;
let
nixos-wsl = import ./nixos-wsl;
in
{
imports = [
"${modulesPath}/profiles/minimal.nix"
nixos-wsl.nixosModules.wsl
];
I would like to know what "${modulesPath} is.
I have tried in shell.
echo ${modulesPath}
nothing
I have tried to print it in a nix interpreter.
nix repl
${modulesPath}
error: syntax error, unexpected DOLLAR_CURLY
modulePath
error: undefined variable 'modulesPath'
nothing too.
Does somebody what is that and more generally how to get the value of "nix constant"
update
I missed something important:
I have to import it in nix repl like this.
nix repl
{modulesPath}: modulesPath
«lambda # (string):1:1»
It say that it is a lamdba. I thought it would give a string value.
Quoting from the nixpkgs source:
For NixOS, specialArgs includes modulesPath, which allows you to import extra modules from the nixpkgs package tree without having to somehow make the module aware of the location of the nixpkgs or NixOS directories.
{ modulesPath, ... }: {
imports = [
(modulesPath + "/profiles/minimal.nix")
];
}
This is performed in nixos/lib/eval-config-minimal.nix, as follows:
lib.evalModules {
inherit prefix modules;
specialArgs = {
modulesPath = builtins.toString ../modules;
} // specialArgs;
};
Because this is done in <nixpkgs>/nixos/lib, ../modules becomes <nixpkgs>/nixos/modules.
$ nix repl
Welcome to Nix 2.8.1. Type :? for help.
nix-repl> "${toString <nixpkgs>}/nixos/modules/profiles/minimal.nix"
"/nix/store/qdblsqzrzarf9am35r6nqnvlsl7dammk-source/nixos/modules/profiles/minimal.nix"
...run this on your own machine, and you'll get a directory that exists for you.

NixOS: how to import some configuration from GitLab? infinite recursion encountered

I'm trying to deploy a NixOS VM while storing its configuration on a private GitLab repository.
My configuration.nix looks like this (simplified to only include the relevant bits):
{ pkgs, ... }:
let
repo = pkgs.fetchFromGitLab { owner = "hectorj"; repo = "nix-fleet"; };
in {
imports = [
./hardware-configuration.nix
"${repo}/my-server-name/host.nix"
];
}
but it is giving me this error:
error: infinite recursion encountered
at /nix/var/nix/profiles/per-user/root/channels/nixos/lib/modules.nix:496:28:
495| builtins.addErrorContext (context name)
496| (args.${name} or config._module.args.${name})
| ^
497| ) (lib.functionArgs f);
I do not understand where the recursion is happening.
It doesn't seem like its even fetching the repo, as I can put any non-existing names in the args and get the same error.
I saw https://nixos.org/guides/installing-nixos-on-a-raspberry-pi.html doing something similar without issue:
imports = ["${fetchTarball "https://github.com/NixOS/nixos-hardware/archive/936e4649098d6a5e0762058cb7687be1b2d90550.tar.gz" }/raspberry-pi/4"];
And I can use that line on my VM and it will build fine.
What am I missing?
The recursion is as follows
Compute the configuration
Compute the config fixpoint of all modules
Find all modules
Compute "${repo}/my-server-name/host.nix"
Compute repo (pkgs.fetch...)
Compute pkgs
Compute config._module.args.pkgs (Nixpkgs can be configured by NixOS options)
Compute the configuration (= 1)
You can break the recursion at 6 by using builtins.fetchTarball instead.
Alternatively, you can break it around 7, by using a different "pkgs".
If you're using configuration.nix as part of a larger expression, you may be able to pass an invoked Nixpkgs to NixOS via specialArgs.pkgs2 = import nixpkgs { ... }. This creates a pkgs2 module argument that can't be configured by NixOS itself.
Otherwise, you could define pkgs2 in a let binding.
{ pkgs, ... }:
let
# pkgs2: An independent Nixpkgs that can be constructed before the NixOS
# imports are resolved.
pkgs2 = import <nixpkgs> {};
repo = pkgs2.fetchFromGitLab { owner = "hectorj"; repo = "nix-fleet"; };
in {
imports = [
./hardware-configuration.nix
"${repo}/my-server-name/host.nix"
];
}

Dart const variables not bootstrapping correctly

I have the following Dart project structure:
myapp/
pubspec.yaml
pubspec.lock
asset/
packages/
build/
web/
MyAppClient.dart
Lookups.dart
Here is MyAppClient.dart:
library myapp;
part "Lookups.dart";
// Load/set environmental variables.
// At runtime SERVER_NAME should be "example.com"
String SERVER_NAME = const String.fromEnvironment(Lookups.ENV_VAR_SERVER_NAME);
// Construct all the URL globals used throughout the application. Each of these Strings should
// be visible everywhere in the "myapp" library
String SERVER_BASE_URL_PATTERN = "http://%s/" + Lookups.APP_NAME;
String SERVER_BASE_URL = SERVER_BASE_URL_PATTERN.replaceAll("%s", SERVER_NAME);
String DO_SOMETHING_URL = SERVER_BASE_URL + Lookups.DO_SOMETHING_SERVICE_ENDPOINT;
void main() {
// Expecting: http://example.com/myapp/doSomething, where "example.com" is the
// "serverName" env var loaded from String.fromEnvironment.
window.alert("DO_SOMETHING_URL = $DO_SOMETHING_URL");
}
And here is Lookups.dart:
part of myapp;
abstract class Lookups {
// Environmental variables.
static const String ENV_VAR_SERVER_NAME = "serverName";
// HTTP/AJAX/URL lookups.
static final String APP_NAME = "myapp";
static final String DO_SOMETHING_SERVICE_ENDPOINT = "/doSomething";
}
When I run pub build and try to run this in a browser (FF v22.0), the HTML does not display correctly and in Firebug I get an Illegal Arguments error. I have a feeling I am not instantiating my global Strings (DO_SOMETHNG_URL, etc.) correctly. Any ideas?
It looks like pub build does not support the -D command line arguments that are used by dart2js to handle environment variables. (You can file an issue).
$ pub build -DserverName=example.com
Could not find an option with short name "-D"

Trying to use Lua module fails on the command line

I'm trying to learn how to use Lua modules. I've been reading the following manual:
http://lua-users.org/wiki/ModulesTutorial
Unfortunately, I can't even get the first example working! I've done the following:
Created a "mymodule.lua" file which looks like this:
local mymodule = {}
function mymodule.foo()
print("Hello World!")
end
return mymodule
Then from the command line, within the folder where the mymodule.lua file resides, I tried to do the following:
mymodule = require "mymodule"
But I get the following error message:
myserver:/usr/share/x/research/# mymodule = require "mymodule"
-ash: mymodule: not found
This works:
myserver:/usr/share/x/research/# local mymodule = require "mymodule"
But then when I try to run the foo() method it fails like so:
myserver:/usr/share/x/research/# mymodule.foo()
-ash: syntax error: bad function name
myserver:/usr/share/x/research/#
And I guess this makes sense because I declared mymodule as local instead of global on the command line.
I guess my question is why can't I declare the module globally on the command line.
The manual says that I should be running this from an "interactive interpreter". I am using a standard commandline / terminal window in linux... could this be the issue? I usually have to prefix all lua commands with "lua ".
Any suggestions would be appreciated.
lua is not your shell. You need to run that code from inside the lua interpeter not at your shell prompt.
myserver:/usr/share/x/research/# lua
Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio
> mymodule = require "mymodule"
> mymodule.foo()

How to compile a Delphi 7 project group file (.bpg) using the command line?

I've grouped a lot of projects in a project group. All the info is in the project.bpg. Now I'd like to automatically build them all.
How do I build all the projects using the command line?
I'm still using Delphi 7.
I never tried it myself, but here is a German article describing that you can use make -f ProjectGroup.bpg because *.bpgs essentially are makefiles.
You can also run Delphi from the command line or a batch file, passing the .bpg file name as a parameter.
Edit: Example (for D2007, but can be adjusted for D7):
=== rebuild.cmd (excerpt) ===
#echo off
set DelphiPath=C:\Program Files\CodeGear\RAD Studio\5.0\bin
set DelphiExe=bds.exe
set LibPath=V:\Library
set LibBpg=Library.groupproj
set LibErr=Library.err
set RegSubkey=BDSClean
:buildlib
echo Rebuilding %LibBpg%...
if exist "%LibPath%\%LibErr%" del /q "%LibPath%\%LibErr%"
"%DelphiPath%\%DelphiExe%" -pDelphi -r%RegSubkey% -b "%LibPath%\%LibBpg%"
if %errorlevel% == 0 goto buildlibok
As I said as a comment to Ulrich Gerhardt answer, the make project_group.bpg is useless if your projects are in subdirectories. Make won't use relative paths and the projects won't compile correctly.
I've made a python script to compile all the DPRs in every subdirectory. This is what I really wanted to do, but I'll leave the above answer as marked. Although it didn't worked for me, It really answered my question.
Here is my script to compile_all.py . I believe it may help somebody:
# -*- coding: utf-8 -*-
import os.path
import subprocess
import sys
#put this file in your root dir
BASE_PATH = os.path.dirname(os.path.realpath(__file__))
os.chdir(BASE_PATH)
os.environ['PATH'] += "C:\\Program Files\\Borland\\Delphi7\\Bin" #your delphi compiler path
DELPHI = "DCC32.exe"
DELPHI_PARAMS = ['-B', '-Q', '-$D+', '-$L+']
for root, dirs, files in os.walk(BASE_PATH):
projects = [project for project in files if project.lower().endswith('.dpr')]
if ('FastMM' in root ): #put here projects you don't want to compile
continue
os.chdir(os.path.join(BASE_PATH, root))
for project in projects:
print
print '*** Compiling:', os.path.join(root, project)
result = subprocess.call([DELPHI] + DELPHI_PARAMS + [project])
if result != 0:
print 'Failed for', project, result
sys.exit(result)
Another vantage of this approach is that you don't need to add new projects to your bpg file. If it is in a subdir, it will compile.

Resources